对于给定的NSRange,我想找到一个CGRect在UILabel对应的的字形NSRange.例如,我想CGRect在"快速的棕色狐狸跳过懒狗"这句话中找到包含"狗"字样的字样.

诀窍是,UILabel有多行,而文本确实attributedText如此,所以找到字符串的确切位置有点困难.
我想在我的UILabel子类上写的方法看起来像这样:
- (CGRect)rectForSubstringWithRange:(NSRange)range;
Run Code Online (Sandbox Code Playgroud)
细节,对于有兴趣的人:
我的目标是能够创建一个具有UILabel的确切外观和位置的新UILabel,然后我可以制作动画.我已经把剩下的事情搞清楚了,但特别是这一步让我暂时退缩了.
到目前为止我尝试解决问题的方法是:
UITextView和UITextField而不是UILabel.我敢打赌,对此的正确答案涉及以下其中一项:
NSLayoutManager和textContainerForGlyphAtIndex:effectiveRange更新:这是一个github要点,我已经尝试了迄今为止解决这个问题的三件事:https://gist.github.com/bryanjclark/7036101
我将Text归为不同的文本颜色.为了更好地阅读突出显示的文本,我使用backgroundcolor.是否有可能在这个Textarea上带来圆角的CALayer效果?不是整个区域,而是内部的特殊文本.
我的代码:
NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"Highlighted Text"
attributes:@{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:10.0f],
NSForegroundColorAttributeName: [UIColor lightGrayColor],
NSBackgroundColorAttributeName: [UIColor darkGrayColor]
}];
Run Code Online (Sandbox Code Playgroud) 我认为我的问题很简单,但我在谷歌上找不到任何东西。我用来NSMutableAttributedString修改字符串中的样式,例如我使用此代码来更改背景颜色:
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor colorWithRed:0.4 green:0.8 blue:1 alpha:1] range:NSMakeRange(0,18)];
Run Code Online (Sandbox Code Playgroud)
我的问题是,我如何才能圆化这个背景的角落?
提前致谢。
Mac OS X上的NSTextContainer有一个方法replaceLayoutManager:用NSLayoutManager的子类替换NSTextView的NSLayoutManager。
不幸的是iOS没有这样的功能。我尝试了这些代码行的组合,但它不断崩溃。
THLayoutManager *layoutManager = [[THLayoutManager alloc] init];
[layoutManager addTextContainer:[self textContainer]];
// [[self textStorage] removeLayoutManager:[self layoutManager]];
//[[self textStorage] addLayoutManager:layoutManager];
[[self textContainer] setLayoutManager:layoutManager];
Run Code Online (Sandbox Code Playgroud)
替换 UITextview 的 NSLayoutManager 的正确步骤是什么?
有没有办法可以围绕每个单词绘制边框UILabel.假设UILabel包含字符串"This is the Line 1".
我想要5个不同的边框,大约5个字
- 这个
- 是
- 该
- 线
- 1
ios ×5
uilabel ×3
ios7 ×2
core-text ×1
iphone ×1
objective-c ×1
quartz-core ×1
swift ×1
textkit ×1
uitextview ×1