我希望我的OHAttributedLabel中的一些单词是链接,但我希望它们是蓝色以外的颜色,我不想要下划线.
这给了我一个带下划线文字的蓝色链接:
-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{
NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy];
[mutableAttributedText beginEditing];
[mutableAttributedText addAttribute:kOHLinkAttributeName
value:[NSURL URLWithString:@"http://www.somewhere.net"]
range:range];
[mutableAttributedText addAttribute:(id)kCTForegroundColorAttributeName
value:color
range:range];
[mutableAttributedText addAttribute:(id)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:kCTUnderlineStyleNone]
range:range];
[mutableAttributedText endEditing];
self.label.attributedText = mutableAttributedText;
}
Run Code Online (Sandbox Code Playgroud)
由于我正在使用OHAttributedLabel,我也尝试使用它的NSAttributedString+Attributes.h类别中的方法,但那些返回蓝色下划线链接:
-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{
NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy];
[mutableAttributedText setLink:[NSURL URLWithString:@"http://www.somewhere.net"] range:range];
[mutableAttributedText setTextColor:color range:range];
[mutableAttributedText setTextUnderlineStyle:kCTUnderlineStyleNone range:range];
self.label.attributedText = mutableAttributedText;
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉在每个版本中设置链接的行,那么文本就会变成我传入的内容 - 这是有效的.似乎设置链接覆盖了这个并将其转回蓝色.
不幸的是,我发现的apple docs页面显示了如何将链接文本设置为蓝色并加下划线,正是我不需要的:https: //developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/任务/ ChangingAttrStrings.html
是否可以应用笔划和填充 a NSAttributedString和a UILabel?
有没有办法从UITextView周围的文本中包装文本UIImage而不使用CoreText?
我一直在玩着没有太多运气的归因字符串,而且CoreText看起来非常复杂,所以我宁愿远离它.
我想使用属性字符串NSLinkAttributeName来UILabel在我的iOS 7项目中的实例内创建可点击链接,现在终于可以在不使用外部库的情况下实现.
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:
url, NSLinkAttributeName, nil];
Run Code Online (Sandbox Code Playgroud)
在字符串上应用该属性会将文本显示为蓝色和带下划线,但单击/点击时不会发生任何操作.为标签启用了用户交互.有人知道怎么做这个吗?谢谢!
我有一个UITextView显示一个NSAttributedString.textView editable和selectable属性都设置为false.
attributionString包含一个URL,我想允许点击URL来打开浏览器.但只有在selectable属性设置为的 情况下才能与URL进行交互true.
如何仅允许用户交互来点击链接,而不是用于选择文本?
我的textViews的格式在iOS 6中运行良好,但不再在iOS 7中运行.我理解使用Text Kit的内容已经发生了很大变化.它变得非常令人困惑,我希望有人可以通过帮助我做一些简单的事来帮助理顺它.
我的静态UITextView最初被分配了一个值textColor和textAlignment属性.然后我做了一个NSMutableAttributedString,为它分配了一个属性,然后将它分配给textView的attributedText属性.对齐和颜色在iOS 7中不再生效.
我怎样才能解决这个问题?如果这些属性不起作用,为什么它们不再存在?这是textView的创建:
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
[self.view addSubview:titleView];
Run Code Online (Sandbox Code Playgroud) 我有一个NSData包含NSAttributedString源自a的属性text()的实例NSTextView.我想将属性字符串转换为普通字符串(NSString)而不进行任何格式化以进行一些文本分析(在转换时我无法访问原始NSTextView及其NSTextStorage实例).
最好的方法是什么?
编辑:
出于好奇,我检查了以下结果:
[[[self textView] textStorage] words]
Run Code Online (Sandbox Code Playgroud)
这对于进行一些文本分析似乎很方便.结果数组包含NSSubTextStorage的实例(下面的单词"Eastern"):
东部{NSFont ="\"LucidaGrande 11.00 pt.P [](0x7ffcaae08330)fobj = 0x10a8472d0,spc = 3.48 \""; NSParagraphStyle ="对齐0,LineSpacing 0,ParagraphSpacing 0,ParagraphSpacingBefore 0,HeadIndent 0,TailIndent 0,FirstLineHeadIndent 0,LineHeight 0/0,LineHeightMultiple 0,LineBreakMode 0,Tabs(\n 28L,\n 56L,\n 84L,\n 112L,\n
140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n
308L,\n \n 336L \n),DefaultTabInterval 0,Blocks(null),Lists(null) ,BaseWritingDirection -1,HyphenationFactor 0,TighteningFactor 0.05,HeaderLevel 0"; }
NSSubTextStorage可能是一个私有类,因为我找不到任何文档.它还保留所有格式.
我想在一个文本块中标注所有®字符的实例(法律免责声明,当然;))并且默认方式NSAttributedString不是很好.
如果我只是让角色成为并且仅使用未经修改的角色NSString,则其呈现与大写字母相同的大小并且大致位于基线处.如果我将superscript属性添加到NSAttributedString如下:
[attrStr setAttributes:@{(NSString *)kCTSuperscriptAttributeName : @1} range:NSMakeRange(locationOfReg, 1)];
Run Code Online (Sandbox Code Playgroud)
角色从基线抬起,其大小不变,但线间距现在受到影响,因为凸起的角色会侵入上面的线.
为了显示:

我在Photoshop中创建了这个图像,通过减小角色的大小和移动基线来实现所需的位置.我知道如何更改iOS中的字体大小,但更改基线似乎更棘手.有关如何实现这一目标的任何建议?
编辑:我想我可以使用上标属性作为一种方法来改变基线.现在,找出一种获取当前字体大小并随后减少它以允许在不同大小的文本块上使用相同方法的方法会很棒.
我有一个参考NSAttributedString,我想更改属性字符串的文本.
我想我必须创建一个新的NSAttributedString并使用这个新字符串更新引用.但是,当我这样做时,我失去了之前字符串的归属.
NSAttributedString *newString = [[NSAttributedString alloc] initWithString:text];
[self setAttributedText:newString];
Run Code Online (Sandbox Code Playgroud)
我参考了旧的属性字符串self.attributedText.如何保留新字符串中的先前属性?