drawInRect text alignment using Swift - ios

I am trying to put some text on top of an image that would be in the center of a CGRect which would be on top of that image. Here is a bit of code:
let textColor: UIColor = UIColor.whiteColor()
let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 17
)!
let textAlignment = NSTextAlignment.Center
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
NSTextAlignment: textAlignment
]
myString.drawInRect(rect, withAttributes: textFontAttributes)
The problem is in the text alignment. When I write it like this I get an error:
Type of expression is ambiguous without more context
When I set key and value types to [String : AnyObject] the compiler complains again:
Cannot convert value of type 'NSTextAlignment.Type' to expected dictionary key type 'String'
Which I understand. I researched this for like two hours and haven't found any up to date solution and not single one how cloud one write this using Swift.

NSTextAlignment isn't a valid key. Note how the other keys end in Name. See the docs for NSFontAttributeName and NSForegroundColorAttributeName to see the list of valid keys.
Instead of NSTextAlignment you need to use NSParagraphStyleAttributeName which requires that you create an instance of NSParagraphStyle. That is where you set the alignment to .Center.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center

The text alignment belongs to the paragraph style. Create a NSMutableParagraphStyle instance and pass it with key NSParagraphStyleAttributeName to the attributes.
let style = NSMutableParagraphStyle()
style.alignment = .Center
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: style
]

Related

UIAlertaction button text alignment bug?

i have a problem with UIAlertAction text alignment , in IOS 10 when language isArabic .
sometimes the action sheet displayed in this way :
and some times it's displayed with this way:
this issue just with IOS 10, how could i set the textAlignmetCenter always ?
Thanks
I have successfully used the following, for both aligning and styling the text of UIAlertControllers:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Center
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody),
NSForegroundColorAttributeName : UIColor.blackColor()
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
You can do a similar thing with the title, if you use "attributedTitle", instead of "attributedMessage"

UITextField stroke attribute opaque in Swift

I am running into a strange issue while working on developing a Meme app. I am trying to create a UITextField that has a black outline with filled white text. I am able to create just a white text field and another that is outlined in black, however I cannot get both simultaneously. Any help would be much appreciated.
Here is the attributes I am assigning to the text field:
let attribs = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeColorAttributeName: UIColor.blackColor(),
NSStrokeWidthAttributeName: 1.0
]
Here is what the app looks like:
Apple has a page describing exactly the problem you were facing: Drawing attributed strings that are both filled and stroked. The trick is to change the stroke width to negative:
Swift 4:
let attributes: [NSAttributedStringKey: Any] = [
.foregroundColor: UIColor.white,
.font: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
.strokeColor: UIColor.black,
.strokeWidth: -1 // Change here
]
Swift 2:
let attributes = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeColorAttributeName: UIColor.blackColor(),
NSStrokeWidthAttributeName: -1 // Change here
]
.strokeWidth = 0 yields fill without stroke.
.strokeWidth = positiveNumber yields stroke without fill.
.strokeWidth = negativeNumber yields both stroke and fill.

Way stroke outline number using swift

is there anyway to draw or stroke a number using swift for iOS? The number has to be in the outline style. Thanks in advance.
Use an NSAttributedString. UITextField can display attributed strings using the attributedText property.
let number = 5 // Whatever number you are looking to output
let font = UIFont.systemFontOfSize(12.0)
let attribs = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: font,
NSStrokeWidthAttributeName: 1.0
]
let outlinedNumber = NSAttributedString(string: "\(number)", attributes: attribs)
Play with the various attributes to get the effect you want.

How do I append two attributed strings with different fonts?

Here I have tried to append two attributed strings in a single UILabel but I made some mistake so any one can find my mistake please?
let textFontAttributes = [
NSFontAttributeName : font,
// Note: SKColor.whiteColor().CGColor breaks this
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
// Note: Use negative value here if you want foreground color to show
NSStrokeWidthAttributeName: -8]
This is the demo, you can have a try, In fact, you should provide more info of what the wrong is or what the error is.
var firstString = "Hello" as NSString
var secondString = " World" as NSString
var totalString = firstString.stringByAppendingString(secondString as String) as NSString
let firsttAttributes = [
NSFontAttributeName : UIFont.systemFontOfSize(16),
// Note: SKColor.whiteColor().CGColor breaks this
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
// Note: Use negative value here if you want foreground color to show
NSStrokeWidthAttributeName: -8]
let secondAttributes = [
NSFontAttributeName : UIFont.systemFontOfSize(12),
// Note: SKColor.whiteColor().CGColor breaks this
NSForegroundColorAttributeName: UIColor.redColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
// Note: Use negative value here if you want foreground color to show
NSStrokeWidthAttributeName: -8]
var attributedString = NSMutableAttributedString(string: totalString as String, attributes: firsttAttributes)
var secondRange = totalString.rangeOfString(secondString as String)
if secondRange.location != NSNotFound {
attributedString.addAttributes(secondAttributes, range: secondRange)
}

how to set attributes in NSAttributedString in iOS?

I'm trying to set the infamous NSFontAttributeName property of an NSAttributedString in iOS but it just doesn't seem to work:
first off, none of the NS constants seem defined for iOS
I read somewhere that I could instead work around it by passing
the CoreText consts instead. Fine... but still, The attribute
expects an NSFont and I'm stuck with UIFont or CTFontRef, neither of
which seems to work:
this doesn't work:
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:16].fontName, [UIFont boldSystemFontOfSize:16].pointSize, NULL);
[myAttString addAttribute:(NSString*)kCTFontNameAttribute
value:(id)ctFont
range:NSMakeRange(0, myAttString.length-1)];
this doesn't work:
[myAttString addAttribute:(NSString*)kCTFontNameAttribute
value:[UIFont boldSystemFontOfSize:16]
range:NSMakeRange(0, myAttString.length-1)];
Is there anyway to make this work?
I found it!
basically, turns out the string constant for the dictionary key I should been using is kCTFontAttributeName
This whole thing is a show...
The NS constants and full attributedString support will be there. Not yet in iOS5 though.
The CoreText constants do work and CTFontRef is the way I use it as well. The first block of your code should work. Can you verify your other bits of code that the problem ain't elsewhere.
do this way:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : UIFont.systemFont(ofSize: 24.0),
NSForegroundColorAttributeName : UIColor.blue,
]
let attrString = NSAttributedString(string: "Stop\nall Dance",
attributes: attributes)

Resources