How to get Text Style to work with Color?

I have been making my app more accessible by using Text Style: Headline and Dynamic Type in all the UILabels and UITextFields. In one part of hte app I want to highlight a label and text field by giving the font a colour as well, which I did in IB. But when the app is running as soon as any new text is entered, the color of the font reverts to black.
Why is this and how do I get what I am trying to do?

Replies

For your labels and text fields in Interface Builder, is the "Text" attribute set to "Plain" or "Attributed"? "Plain" formatting gives to the entire label/text field the same font and color (like a raw text file), but "Attributed" formatting allows for different fonts in different parts of the text (like a document).


I suspect that your text field is set to "Attributed", and removing text resets the font color. If so, set your text field to "Plain" and then verify that the font and color are correct.

They both have Text: Plain, I'm afraid.

I just created a test app and tried what you described, but I did not find any problems. Here is the storyboard source code:


<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Test" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="Test" borderStyle="roundedRect" textAlignment="natural" adjustsFontForContentSizeCategory="YES" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="DwI-bw-mzS">
                                <rect key="frame" x="0.0" y="20" width="375" height="30"/>
                                <color key="textColor" red="1" green="0.14913141730000001" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                <fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
                                <textInputTraits key="textInputTraits"/>
                            </textField>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="DwI-bw-mzS" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="0Y5-TZ-5Ju"/>
                            <constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="DwI-bw-mzS" secondAttribute="trailing" id="Gbw-oR-Mfj"/>
                            <constraint firstItem="DwI-bw-mzS" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="XrZ-sF-Hpl"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
        </scene>
    </scenes>
</document>

Do you make any appearance changes to the UITextField in code? Maybe you could try logging the UITextField's text color at different places in your code:

NSLog(@"text color: %@", self.textField.textColor); // Objective-C
print("text color: \(String(describing: textField.textColor))") // Swift