我打算使用NSAttributedString来突出显示用户搜索的匹配查询的字符串部分.但是,我找不到iOS的等价物NSBackgroundColorAttributeName- 没有kCTBackgroundColorAttributeName.难道这样的事情存在,类似的方式NSForegroundColorAttributeName变kCTForegroundColorAttributeName?
我试图从UITextView(和/或UITextField)获取选定的文本范围,以便我可以编辑选定的文本,或修改属性字符串.我做出选择时会触发下面的方法,但方法中的代码会返回空值.
- (void)textViewDidChangeSelection:(UITextView *)textView {
UITextRange *selectedRange = [textField selectedTextRange];
NSLog(@"Start: %@ <> End: %@", selectedRange.start, selectedRange.end);
}
Run Code Online (Sandbox Code Playgroud) 我有一个NSTextAlignmentJustified用于的应用程序NSAttributedString.在iOS 6中,一切都很好.但是在iOS 7中运行的相同App (模拟器或设备没有任何区别)显示没有任何Justify.此外,从iOS 6到7,线条间距似乎发生了巨大变化.
其他人遇到过这个问题?有没有办法在iOS 7中制作合理的Textblock(在iOS 6中也能正常工作?)
此致,马库斯
在iOS中呈现快速更改文本(10-15个数字可以改变每一帧)的最高效的方法是什么?这些是我到目前为止所做的尝试:
1)CATextLayer+ CTFont和NSString:相当快,但无法访问我需要的字距调整.(如果有人知道如何使用CTFont和不使用kerning的技巧NSAttributedString,这也很酷,但这不是主要问题;))
2)CATextLayer+ NSAttributedString:缓慢且滞后.与1)相比,我看到了巨大的FPS下降.在我的应用程序中,此更改使FPS从50-60降至30.
3)位图字体,使用UIImageViews和UIImage显示数字:虽然不是很令人满意,但是在iOS中应该有更好的方式/更自然的方式来有效地渲染字体.
编辑:
4)UILabel+ NSString- 相当快,但再次无法访问字距.
5)UILabel+ NSAttributedString- 再次缓慢而迟缓.
渲染NSAttributedStringa比渲染基本要慢得多NSString.这真是令人沮丧,因为我现在需要NSAttributedString的只是字母之间的间距.
我有一个UITextView,并且我正在使用NSString stringWithFormat投射某些单词,我想加粗.
我已经浏览了Stack Overflow并试图关注帖子,但我想我不理解它.
这是我一直在玩的东西:
NSRange boldedRange = NSMakeRange(0, 4);
NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
self.resultsTextView.attributedText = attrString;
self.resultsTextView.text = [NSString stringWithFormat:@"One day, %@ was taking a walk and saw a %@ boy. He was %@ a %@.", attrString, self.adjective, self.adverb, self.noun];
Run Code Online (Sandbox Code Playgroud) objective-c uitextview nsattributedstring ios nsmutableattributedstring
我有一个UILabel,在某些情况下文本比UILabel自身长,所以我看到文本,因为"bla bla bla..."我想...Read More在结尾添加一个按钮文本UILabel..
我已经阅读了一些帖子,但是他们提供的解决方案对我来说并不好,例如:计算将输入多少个字符UILabel,但是我使用的字体每个字符都有不同的宽度.
我怎么能设法做到这一点?
提前致谢!
我在按钮attributesTitle中添加了一些属性
let attr = NSMutableAttributedString(string: currTitle)
attr.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attr.length))
attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attr.length))
currButton?.setAttributedTitle(attr, forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
按钮点击后如何从中删除NSStrikethroughStyleAttributeName?
setattribute nsattributedstring ios nsmutableattributedstring swift
在一个NSAttributedString字母中,一系列字母具有链接属性和自定义颜色属性.
在带有Swift 2的Xcode 7中,它可以工作:
在带有Swift 3的Xcode 8中,链接的自定义属性颜色始终被忽略(截图中应为橙色).
这是测试代码.
Swift 2,Xcode 7:
import Cocoa
import XCPlayground
let text = "Hey @user!"
let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orangeColor(), range: range)
attr.addAttribute(NSLinkAttributeName, value: "http://somesite.com/", range: range)
let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.selectable = true
tf.stringValue = text
tf.attributedStringValue = attr
XCPlaygroundPage.currentPage.liveView = tf
Run Code Online (Sandbox Code Playgroud)
Swift 3,Xcode 8:
import Cocoa
import PlaygroundSupport
let text …Run Code Online (Sandbox Code Playgroud) 编辑
请参阅我的答案以获得完整的解决方案:
我设法通过使用UITextView而不是a 来解决这个问题UILabel.我写了一个类,使UITextView行为像一个UILabel但具有完全准确的链接检测.
我已经成功设置了链接的样式而没有使用问题,NSMutableAttributedString但我无法准确地检测到哪个字符已被点击.我已经尝试了这个问题中的所有解决方案(我可以转换为Swift 4代码),但没有运气.
以下代码有效,但无法准确检测单击了哪个字符并获取了链接的错误位置:
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize.zero)
let textStorage = NSTextStorage(attributedString: label.attributedText!)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
// Configure textContainer
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
let labelSize = label.bounds.size
textContainer.size = labelSize
// Find the tapped …Run Code Online (Sandbox Code Playgroud) 我WKInterfaceLabel在运行watchOS 5的Apple Watch应用程序中有一个.我将其属性字符串设置为包含应用于子字符串的粗体字体属性的字符串.运行应用程序时,粗体显示正确.但是,如果您在iPhone上的Watch应用程序中更改动态类型的大小,则手表上的文本大小会更改,但粗体消失.
我希望watchOS只是设置WKInterfaceLabel消除粗体字体的字体.保留其他属性,例如前景色.
我注意到NSNotification.Name.didChangeNotificationWatchKit不支持,所以我不能拦截这个大小的变化.
如何处理WatchKit中的动态类型更改?有没有办法在用户更改文本大小时保留属性字符串中的字体属性?
nsattributedstring swift apple-watch watchkit wkinterfacelabel
ios ×7
objective-c ×4
swift ×3
core-text ×2
uilabel ×2
uitextview ×2
apple-watch ×1
cocoa ×1
ios7 ×1
nstextfield ×1
performance ×1
setattribute ×1
swift4 ×1
uitextfield ×1
uitextrange ×1
watchkit ×1
xcode ×1
xcode8 ×1