xcode 在界面生成器中使用“属性”标签文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25876609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using 'attributed' label text in Interface Builder
提问by Kyle G
I'm attempting to forego using NSAttributedString in my viewController, opting to use the 'Attributed' option (as apposed to 'Plain') in the 'text' field of the attributes inspector for a UILabel.
我试图放弃在我的 viewController 中使用 NSAttributedString,选择在 UILabel 的属性检查器的“text”字段中使用“Attributed”选项(与“Plain”相对应)。
I've selected the individual words I want to attribute, and have changed their font, this is reflected correctly in the text preview window and on the scenes in IB itself:
我选择了我想要属性的单个单词,并更改了它们的字体,这在文本预览窗口和 IB 本身的场景中正确反映:
However, the simulator does not reflect these changes, it seems as though at runtime the font specified for the attributes is not used (when I change a color, this works as expected). I've added the fonts to my project and to my plist to no avail:
但是,模拟器并未反映这些更改,似乎在运行时未使用为属性指定的字体(当我更改颜色时,这按预期工作)。我已将字体添加到我的项目和我的 plist 中,但无济于事:
I've found no documentation on this feature and it may very well be similar to the 'text styles' feature in IB (essentially useless for now) - but just wanted to see if anyone had had any luck doing something similar in the past.
我没有找到关于此功能的文档,它可能与 IB 中的“文本样式”功能非常相似(现在基本上没用)-但只是想看看过去是否有人有幸做过类似的事情。
Any help or suggestions is much appreciated, thanks.
非常感谢任何帮助或建议,谢谢。
Edit 03/07/15Well, I'm pleased to report that this issue is finally resolved in Xcode 7 (still in beta). So we can finally put this issue to rest. Thanks to everyone who tried to figure this out along with me.
编辑 03/07/15好吧,我很高兴地报告这个问题终于在 Xcode 7(仍处于测试阶段)中得到解决。所以我们终于可以解决这个问题了。感谢所有和我一起试图解决这个问题的人。
回答by Jamil
In Xcode 7.0 beta: I found this is working.
在 Xcode 7.0 beta 中:我发现这是有效的。
Storyboard: http://imgur.com/Wb5H4ds
故事板:http: //imgur.com/Wb5H4ds
Output:
输出:
OR.You can do it dynamically.
或者。你可以动态地做到这一点。
-(void)attributedText{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 100, 200, 44)];
NSString *newsTitle = @"Hello";
NSString *sportTtle = @"World";
NSString *title = [NSString stringWithFormat:@"%@ %@", newsTitle,sportTtle];
textView.text = title;
UIColor *color = [UIColor redColor];
UIFont *font = [UIFont fontWithName:@"Arial" size:20.0];
NSDictionary *attrs = @{NSForegroundColorAttributeName : color,NSFontAttributeName:font};
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
[attrStr addAttributes:attrs range:[textView.text rangeOfString:sportTtle]];
textView.attributedText = attrStr;
[self.view addSubview:textView];
}
回答by Silversky Technology
Try This
尝试这个
In my case when i try to set "Silversky Technology" as Attributed text for label from interface builder its not show when i run in simulator but its show in interface builder. So i used one trick i made Silversky font with 1 pixel bigger then Technology text.
在我的情况下,当我尝试将“Silversky Technology”设置为来自界面构建器的标签的属性文本时,当我在模拟器中运行时它不会显示,但它会在界面构建器中显示。所以我使用了一种技巧,我使 Silversky 字体比技术文本大 1 个像素。
Attribute text have issue with same size of fontso change size of 1 word this thing work with me.
属性文本具有相同大小的字体问题,因此更改 1 个单词的大小这件事对我有用。
May be this is xcode bug but this trick work for me.
可能这是 xcode 错误,但这个技巧对我有用。
回答by Eugene Braginets
Fonts issue. Workaround for this: clean / clean build folder. Sometimes help. /UPDATE: I know this sounds like a "stupid advice" - but it made a trick in my case, so posting it here - to save someone's time maybe.
字体问题。解决方法:清理/清理构建文件夹。有时帮助。/更新:我知道这听起来像是一个“愚蠢的建议”——但它在我的情况下做了一个伎俩,所以把它张贴在这里——也许是为了节省某人的时间。
回答by Hamid Reza Ansari
that's have a simple and quick solition and that's work in my case . that solution is add a code line in didFinishLaunchingWithOptions func in AppDelegate.swift file :
这是一个简单而快速的解决方案,在我的情况下是有效的。该解决方案是在 AppDelegate.swift 文件中的 didFinishLaunchingWithOptions 函数中添加一行代码:
for textViews:
对于文本视图:
UITextView.appearance().font = UIFont(name: "IranSans", size: 17)
for labels:
对于标签:
UILabel.appearance().font = UIFont(name: "IranSans", size: 17)
and for rest of UiView like this two ??
和其余的 UiView 像这两个 ??
回答by anthonyliao
I got this working in xcode 6.1. I added a UILabel with words "Hello World", and changed the font to Didot.
我在 xcode 6.1 中得到了这个。我添加了一个带有“Hello World”字样的 UILabel,并将字体更改为 Didot。
Here's the xml code of my storyboard. Match this against yours and see if anything is awry
这是我的故事板的 xml 代码。将此与您的相匹配,看看是否有任何问题
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" usesAttributedText="YES" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Cd6-WA-P1d">
<rect key="frame" x="20" y="91" width="275" height="21"/>
<attributedString key="attributedText">
<fragment content="Hello World">
<attributes>
<color key="NSColor" cocoaTouchSystemColor="darkTextColor"/>
<font key="NSFont" size="24" name="Didot"/>
<paragraphStyle key="NSParagraphStyle" alignment="right" lineBreakMode="wordWrapping" baseWritingDirection="natural"/>
</attributes>
</fragment>
</attributedString>
<nil key="highlightedColor"/>
</label>
回答by emp
I was trying to do this with attributed labels in a basic style table view cell.
As soon as you choose the "Attributed" option in Interface Builder, your custom fonts don't appear in the font picker.
我试图在基本样式表视图单元格中使用属性标签来做到这一点。
一旦您在 Interface Builder 中选择了“Attributed”选项,您的自定义字体就不会出现在字体选择器中。
You might notice that the font picker is the default system font picker.
您可能会注意到字体选择器是默认的系统字体选择器。
The trick is to install the fonts into /Library/Fonts. Now they will appear in the fonts dialog when you change the label to Attributed, as well as in the storyboard view and preview views.
诀窍是将字体安装到 /Library/Fonts 中。现在,当您将标签更改为 Attributed 时,它们将出现在字体对话框中,以及故事板视图和预览视图中。
However, this stilldoesn't work at runtime. And if you view the storyboard on another computer without the font installed, Interface Builder will revert to a "close match" (Lucida Grande in my case).
但是,这在运行时仍然不起作用。如果您在未安装字体的另一台计算机上查看故事板,Interface Builder 将恢复为“接近匹配”(在我的情况下为 Lucida Grande)。
There is one advantage to all of this: If you are experimenting with fancy formatting, it's nice to see it live in Interface Builder. Then you can look at the source for the storyboard, and copy the attributes into source code.
Dont' forget to reset your label to "Plain" in Interface Builder, else the "close match" issues occurs as described above.
所有这一切都有一个好处:如果您正在尝试花哨的格式,很高兴在 Interface Builder 中看到它。然后您可以查看故事板的源代码,并将属性复制到源代码中。
不要忘记在 Interface Builder 中将您的标签重置为“Plain”,否则会出现如上所述的“接近匹配”问题。
回答by Hung Nguyen
Do you change text of that label in code. When use config attribute of label in Interface Builder, Don't change any thing relate to this label in code.
您是否在代码中更改了该标签的文本。在 Interface Builder 中使用标签的 config 属性时,不要更改代码中与此标签相关的任何内容。