【发布时间】:2019-07-15 11:11:14
【问题描述】:
【问题讨论】:
标签: ios swift cocoa-touch uitextview
【问题讨论】:
标签: ios swift cocoa-touch uitextview
【讨论】:
https://stackoverflow.com/a/26456563/8272698'
我在上面找到了这个答案,不幸的是它在目标c中
但是您仍然可以看到他们是如何做到的。
- (void)highlight {
NSRange selectedTextRange = self.textView.selectedRange;
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor redColor]
range:selectedTextRange];
float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if (sysVer < 8.0) {
// iOS 7 fix
self.textView.scrollEnabled = NO;
self.textView.attributedText = attributedString;
self.textView.scrollEnabled = YES;
} else {
self.textView.attributedText = attributedString;
}
}
基本上,他们创建了一个名为 highlight 的函数。
在用户实际突出显示所选部分时,他们正在寻找的功能中。 self.textView.selectedRange 当他们获得用户选择的文本范围时。他们给它一个属性并为其提供颜色。
斯威夫特
let selectedText = textView.selectedRange
创建属性:
let myAttribute = [ NSForegroundColorAttributeName: selectedUIColor]
提供你的属性,
textView.textStorage.addAttributes(myAttribute, range: selectedText)
在发件人调用的操作中使用它。
希望这会有所帮助!
【讨论】: