Error When Instantiating a UIfont in an Text Attributes Dictionary

Error when instantiating a UIFont in an text attributes dictionary

The initializer of UIFont returns an optional because it may fail due to misspelled font name etc.

You have to unwrap it and check:

if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}

UPDATED for Swift 3

if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}

Xcode 6.1 titleTextAttributes

I think the problem is because they've changed UIFont's initializer in 6.1 so it can return nil. That is correct behavior because if you enter wrong font name there is no way to instantiate UIFont. In this case your dictionary becomes [NSObject: AnyObject?] which is not the same with [NSObject: AnyObject]. You can first initialize fonts and then you can use if let syntax. Here is how to do this

let font = UIFont(name: "SourceSansPro-Regular", size: 22)
if let font = font {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : font, NSForegroundColorAttributeName : UIColor.whiteColor()]
}

Or if you sure the font object is not gonna be nil you can use implicitly unwrapped optional syntax. In this case you are taking the risk of runtime crash. Here is how to do it.

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : UIFont(name: "SourceSansPro-Regular", size: 22)!, NSForegroundColorAttributeName : UIColor.whiteColor()]

Cannot invoke 'init' with an argument list of type '($T7, forState: UIControlState)' in swift

This is because arrayImage[i] is not String. The arrayForKey method returns an optional Array of AnyObjects ([AnyObjects]?). You have to convert it to a String array before using it. Try this code

if let arrayImage = NSUserDefaults.standardUserDefaults().arrayForKey("ImageArray") as? [String] {
btnImage.setBackgroundImage(UIImage(named: arrayImage[0]), forState: UIControlState.Normal)
}

Unexpectedly found nil while unwrapping an Optional value', despite every property having a value

Something was indeed being force unwrapped, however it was none of my variables.

Firebase reference is initially set as

ref: DatabaseReference!

In self.ref.child, ref needs a ?:

self.ref?.child

Of course, the console didn't reveal that all too well. So the ref was being force unwrapped, and I wasn't checking to make sure it existed when I used it in my handleRegister() function.

I also forgot to instantiate my Firebase reference in viewDidLoad(). /p>

Set line height in UITextView

After iOS 7, the styleString approach no longer works.

Two new alternatives are available.

Firstly, TextKit; a powerful new layout engine. To change line spacing, set the UITextView's layout manager's delegate:

textView.layoutManager.delegate = self; // you'll need to declare you implement the NSLayoutManagerDelegate protocol

Then override this delegate method:

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 20; // For really wide spacing; pick your own value
}

Secondly, iOS 7 now supports NSParagraphStyle's lineSpacing. This gives even more control, e.g. first line indentation, and calculation of a bounding rect. So alternatively...

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 15; // <--- indention if you need it
paragraphStyle.firstLineHeadIndent = 15;

paragraphStyle.lineSpacing = 7; // <--- magic line spacing here!

NSDictionary *attrsDictionary =
@{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, e.g NSFontAttributeName

self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];

FWIW, the old contentInset method to align the text along the left edge of UITextView is also no use under iOS7. Instead, to remove the margin:

textView.textContainer.lineFragmentPadding = 0;


Related Topics



Leave a reply



Submit