我尝试为字符串的一部分加下划线,例如,' test string '字符串中的' string '部分.我正在使用,我的解决方案运作良好.NSMutableAttributedStringiOS7
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString:@"test string"];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(5, 6)];
myLabel.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
问题是我的解决方案iOS8不再适用了.在花了一个小时测试多个变体之后NSMutableAttributedString,我发现这个解决方案只在范围从0开始时才有效(长度可以不同).这是什么原因?我该如何解决这个问题?
如何强调UILabel仅使用故事板的部分文本?我能够在代码中执行此操作,并且我能够为标签的整个文本加下划线,但不仅仅是字符串中的一个或两个单词.
我是iOS开发的新手.
我有一个标签,LatestInfo这里有文字,并且有一个链接到网站:例如For the latest information visit example.com/latestInfo
我希望显示为URL加下划线example.com/latestInfo并使其可点击.
我使用的是Swift而不是Obejective-C
我该怎么做呢?
根据皮埃尔的要求编辑:
@IBOutlet weak var linkLabel: UITextView!
let string = "A great link : Google"
let range = (string as NSString).rangeOfString("Google")
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)
linkLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud) 我试图使用NSMutableAttributedString与下面的虚线下划线是我的代码,但没有一个模式似乎工作,我错过了什么?
var str : NSMutableAttributedString = NSMutableAttributedString(string: "HelloWorld")
str.addAttribute(NSUnderlineStyleAttributeName , value: NSNumber(integer:(NSUnderlineStyle.PatternDot).rawValue), range: NSMakeRange(0, str.length))
Run Code Online (Sandbox Code Playgroud) 我有一个 UILabel 我想将它的某些部分作为下划线。我正在使用下面的代码。但它强调整个标签而不是标签的某些部分。请告诉我如何使标签的某些部分成为下划线。
let attributedString = NSMutableAttributedString(string: "UserAgreement".localized)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: 10))
labelTc.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
我不想处理故事板
ios ×5
swift ×3
iphone ×2
uilabel ×2
attributes ×1
click ×1
ios7 ×1
ios8 ×1
objective-c ×1
range ×1
storyboard ×1
string ×1
text ×1