Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何保持用户输入的普通文本格式不变,而不是随最后一个字符的Attribute变化? #254

Open
goodbless opened this issue Mar 28, 2016 · 6 comments

Comments

@goodbless
Copy link

您好,我现在在开发一个类微博的文本编辑界面,用户输入的文本中会插入@或是话题#,这些特殊的文本片段的字体颜色区别于普通文本,但现在遇到一个问题,就是用户紧接着这些特殊格式文本输入,后面的文字都变成了前面特殊文字的格式了,有没有办法解决这个问题?

@ibireme
Copy link
Owner

ibireme commented Mar 28, 2016

可以参考类似的问题,例如 #241

@goodbless
Copy link
Author

我看了,默认的继承左侧字符Attribute的行为确有其合理性,但在移动端做类似word、page等重量级富文本编辑的情景比较少,大部分情景是用户输入默认格式文本,间隔插入一些特殊格式文本,所以能有一个这个行为是否启用的开关就好了

@ibireme
Copy link
Owner

ibireme commented Mar 28, 2016

这是 NSMutableAttributedString 本身的逻辑,并不是我有意实现的,我也没办法修改这个逻辑。。

@goodbless
Copy link
Author

在NSAttributedString+YYText 中有 removeDiscontinuousAttributesInRange:allDiscontinuousAttributeKeys 方法,这些不连续属性不会被插入的字符串自动继承。我建议这个地方可以增加一个 YYCustomDiscontinuousAttributeName 的key,或者将这个地方开放接口允许用户自定义他们自己的不连续属性,我觉得是一个挺实用的改动。

@ibireme
Copy link
Owner

ibireme commented Mar 29, 2016

稍后会考虑一下提供接口。

@ZengyiMa
Copy link

ZengyiMa commented Apr 16, 2016

@ibireme @goodbless 我现在就在做类似这类可以编辑简单样式的富文本编辑器,这个问题我现在的处理是替换YYTextView的输入控制那部分,大概在1446行左右,有个

 [_innerText replaceCharactersInRange:range.asRange withString:text];

这个方法默认是继承左边样式,替换成

    [_innerText replaceCharactersInRange:range.asRange withAttributedString:[[NSAttributedString alloc]initWithString:text]];

把样式设置为你默认的,或者你现在设置的样式。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants