34
votes

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.

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:

enter image description here

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:

enter image description here

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.

Any help or suggestions is much appreciated, thanks.

Edit 03/07/15 Well, 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.

8
please give your Xcode versionJamil
Were you using size classes for the label in the interface builder? I mean were you trying to set different font sizes for different size class via the interface builder?Sumeet
I'm still having this problem in Xcode 7.1 (7B91b).Gordonium
This problem is still present in Xcode 7.3 as well.Yevgeniy Leychenko

8 Answers

4
votes

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.

Attribute text have issue with same size of font so change size of 1 word this thing work with me.

May be this is xcode bug but this trick work for me.

3
votes

In Xcode 7.0 beta: I found this is working.

Storyboard: http://imgur.com/Wb5H4ds

Output:

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

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.

0
votes

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 :

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

0
votes

My initial issue:

Custom font, included and bundled with the project. XCode Version 11

Used a UILabel with attributed text, changing use of normal font and italic version.

The Interface Builder showed normal and italic font in a single UILabel - the Simulator and the testing device only showed normal font

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. My text was all white. I manipulated all italics to a 99% white and all normal fonts to 100% white.

<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

-2
votes

I got this working in xcode 6.1. I added a UILabel with words "Hello World", and changed the font to Didot.

Here's the xml code of my storyboard. 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>
-2
votes

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.

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.

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

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.

-3
votes

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.