简体   繁体   English

"在 Interface Builder 中使用“属性”标签文本"

[英]Using 'attributed' label text in Interface Builder

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 的属性检查器的“文本”字段中使用“属性”选项(与“普通”相对)。

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\/15<\/strong> Well, I'm pleased to report that this issue is finally resolved in Xcode 7 (still in beta).编辑 03\/07\/15<\/strong>好吧,我很高兴地报告这个问题最终在 Xcode 7 中得到解决(仍处于测试阶段)。 So we can finally put this issue to rest.所以我们终于可以解决这个问题了。 Thanks to everyone who tried to figure this out along with me.感谢所有试图和我一起解决这个问题的人。

"

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.在我的情况下,当我尝试将“Silversky Technology”设置为来自界面构建器的标签的属性文本时,它在模拟器中运行时不显示,但在界面构建器中显示。 So i used one trick i made Silversky font with 1 pixel bigger then Technology text.所以我使用了一种技巧,我使 Silversky 字体比技术文本大 1 个像素。

Attribute text have issue with same size of font so change size of 1 word this thing work with me.属性文本具有相同大小的字体问题,因此更改 1 个单词的大小这件事对我有用。

May be this is xcode bug but this trick work for me.可能这是 xcode 错误,但这个技巧对我有用。

In Xcode 7.0 beta: I found this is working.在 Xcode 7.0 beta 中:我发现这是有效的。

Storyboard: http://imgur.com/Wb5H4ds故事板: http : //imgur.com/Wb5H4ds

Output:输出:

http://imgur.com/iOV8tJF http://imgur.com/iOV8tJF

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];
}

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. /更新:我知道这听起来像是一个“愚蠢的建议”——但它在我的情况下做了一个伎俩,所以把它张贴在这里——也许是为了节省某人的时间。

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 的其余部分,就像这两个 ☝️

My initial issue:我最初的问题:

Custom font, included and bundled with the project.自定义字体,包含并与项目捆绑在一起。 XCode Version 11 XCode 版本 11

Used a UILabel with attributed text, changing use of normal font and italic version.使用带有属性文本的 UILabel,改变了普通字体和斜体版本的使用。

The Interface Builder showed normal and italic font in a single UILabel - the Simulator and the testing device only showed normal font Interface Builder 在单个 UILabel 中显示正常和斜体字体 - 模拟器和测试设备仅显示正常字体

Cleaning build folder did not help.清理构建文件夹没有帮助。

My working (workaround) solution:我的工作(解决方法)解决方案:

I opended the Interface Builder file (*.storyboard) with a text editor and manipulated the color attributes.我用文本编辑器打开了 Interface Builder 文件 (*.storyboard) 并操作了颜色属性。 My text was all white.我的文字是全白的。 I manipulated all italics to a 99% white and all normal fonts to 100% white.我把所有的斜体都改成 99% 的白色,把所有普通字体改成 100% 的白色。

<color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>

and

<color key="NSColor" red="0.99" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>

Summary概括

Try to make invisible alternating differences (to properties) between parts of texts with changing fonts - use a text editor尝试在具有变化字体的文本部分之间制作不可见的交替差异(属性) - 使用文本编辑器

The only thing that worked for me was changing the font, so here's what I did:唯一对我有用的是更改字体,所以这就是我所做的:

  1. Used SF Pro Text for the entire text and Arial for the text I wanted to separate.对整个文本使用SF Pro Text ,对我想要分离的文本使用Arial
  2. Used NSMutableAttributedString.enumerateAttributes to find the Arial font and modify it the way I wanted to.使用NSMutableAttributedString.enumerateAttributes找到Arial字体并按照我想要的方式修改它。
  3. Changed the font back to SF Pro Text将字体改回SF Pro Text

Hope it helps somebody.希望它可以帮助某人。

I got this working in xcode 6.1.我在 xcode 6.1 中得到了这个。 I added a UILabel with words "Hello World", and changed the font to Didot.我添加了一个带有“Hello World”字样的 UILabel,并将字体更改为 Didot。

Here's the xml code of my storyboard.这是我的故事板的 xml 代码。 Match this against yours and see if anything is awry将此与您的相匹配,看看是否有任何问题

<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>

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.诀窍是将字体安装到 /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.现在,当您将标签更改为 Attributed 时,它们将出现在字体对话框中,以及故事板视图和预览视图中。

However, this still doesn'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.所有这一切都有一个好处:如果您正在尝试花哨的格式,很高兴在 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 中将您的标签重置为“Plain”,否则会出现如上所述的“接近匹配”问题。

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 属性时,不要更改代码中与此标签相关的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM