I am trying to help changing some styling in an IOS-application developed using Swift and with a storyboard.
This is how the page look
and the product owner would like to make the findtoilet#findtoilet.dk larger, but it seems that changing the font and styling in the storyboard has no effect because IOS decides the formatting of the email
Any ideas?
Using this
// Define general attributes for the entire text
NSDictionary *attribs = #{
NSForegroundColorAttributeName: self.label.textColor,
NSFontAttributeName: self.label.font
};
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:attribs];
Will take the style defined for the label on the storyboard and apply that, thus overruling IOS-styling for a tappable link
Related
I want same textfield or any control in iOS Objective-C, like this image
google wallet input entry.
Yes totally possible Use NSAttributed String here is the link NSAttributedString
Create 2 NSDictionary one for smaller fontSize and one for Bigger fontSize.
in the small font size Attributed Dictionary the size for the NSBaselineOffsetAttributeName is used to move the small font up or down change the value to see the effect.
// I have added the attribute for font color as well so it should look the same as picture above
NSDictionary *smallFontSize = #{
NSFontAttributeName : [UIFont systemFontOfSize:10],
NSBaselineOffsetAttributeName : [[NSNumber alloc] initWithInt:10],
NSForegroundColorAttributeName : [UIColor grayColor]
};
Second dictionary is very simple it only contains the font Size
NSDictionary *bigFontSize = #{
NSFontAttributeName : [UIFont systemFontOfSize:25]
};
Declare NSMutableAttributedString not AttributedString pass in the samllFontSize
// I have directly passed the $ sign but you will pass it as a property
NSMutableAttributedString *mutableAttriString = [[NSMutableAttributedString alloc] initWithString:#"$" attributes:smallFontSize];
Now create two new AttributedStrings these 2 can be attributedString because we are not manipulating them
// You will be passing the amount and decimals as properties
NSAttributedString *amount = [[NSAttributedString alloc] initWithString:#"10" attributes:bigFontSize];
NSAttributedString *decimalAmount = [[NSAttributedString alloc] initWithString:#"00" attributes:smallFontSize];
Now try to append both amount and decimalAmount to the mutableAttributedString that was the reason we made the first one mutable so we can append to it later
[mutableAttriString appendAttributedString:amount];
[mutableAttriString appendAttributedString:decimalAmount];
Each label has a property called text and another one called attributedText add it to the attributedText property
self.priceLabel.attributedText = mutableAttriString
Here is the screenshot of the label change the font and colours to your liking.
I have a UITextView where user enter data. I have a format bar below which has
bold, italic, underline and other options like alignment etc.
After a lot of search and testing I came to know that NSString don't support such bold, italic styling instead NSAttributedString should be used but I use NSAttributedString I am unable to make the selected text bold and italic at the same time.
My code is like this
NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if(textBoldBtn){
UIFont *boldFont = [UIFont boldSystemFontOfSize:textView.font.pointSize];
NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
[dict addEntriesFromDictionary:boldAttr];
}
if(textItalicBtn){
UIFont *italicFont = [UIFont italicSystemFontOfSize:textView.font.pointSize];
NSDictionary *italicAttr = [NSDictionary dictionaryWithObject:italicFont forKey:NSFontAttributeName];
[dict addEntriesFromDictionary:italicAttr];
}
attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:dict];
[textViewText appendAttributedString:attributedText];
textView.attributedText = textViewText;
return true;
}
Using this technique it only takes the last font in dict when both buttons are pressed.
I have gone through a link on stack overflow where they use fontDescriptor of Label to make the text both italic and bold but note I don't want to use any label. I am using UITextView. I have also gone through
[textView setAllowsEditingTextAttributes:YES];
But I want my own functions. I have also seen several EGOTextView and TextEdit for iOS but I strictly want this for iPad. I only want to use UITextView.
Kindly tell if there is any way using attributed string to make text both italic bold and even underline at the same time or any way to customize the functions of textView personal EditingTextAttributes functionality.
Thanks in Advance.
*App has to be uploaded to app store so no private frameworks required because they would cause rejection of app.
You have a some issues.
First:
Attributes are a NSDictionary. That means works with key/value, and the key is unique!
So when, you use addEntriesFromDictionary:, the doc says:
If both dictionaries contain the same key, the receiving dictionary’s
previous value object for that key is sent a release message, and the
new value object takes its place.
And when you want to apply bold and italic effect in your code, you think that you have:
Key — Value
NSFontAttributeName — theBoldFont
NSFontAttributeName — theItalicFont
Whereas you replace the bold font with the italic one.
Second:
Since Bold AND Italic are a attribute "hidden" in UIFont, if you want to apply the both effect, you have to find a font which is italic AND bold. That why, underlining (NSUnderlineAttributeName) can be added without encountering your issue with bold and italic.
You may look there to know how to do about it.
So you may code some logic like this:
if (textItalicBtn && textBoldBtn)
{}
else if (textItalicBtn)
{}
else if (textBoldBtn)
{}
It appears that this has been asked and not answered before, but the question is ancient and there have been many Xcode / iOS updates since then, so I am going to give this a shot.
I have a simple view controller laid out. There is a single View that contains a read-only Text View with some instructions on how to use the app. I would like to intersperse some images in the scrolling text view to refer to the buttons and other elements that I am referring to in the instructions.
Here is the view:
So for instance, when the instructions refer to the green start button, I would like to insert an image of that button inline with the rest of the text.
I am using Xcode 5.1.1 and of course storyboards. Thanks in advance for any suggestions.
You can use the NSAttributedString with NSTextAttachment to attach an image to the text
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"press to start"];
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = [UIImage imageNamed:#"AnyImage.png"];
NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:stringWithImage];
self.textView.attributedText = attributedString;
I have the following HTML in a UITextView and would like to render it into a UITextView
is my body for the note
food item - more item stuff;`
Let me add: it's currently showing as blue and underlined and not clickable. I would like to make it bolded and not clickable. I have read the docs regarding linkTextAttributes but, not having used this, it is a bit beyond me and I don't really see any easy way to manipulate this. How would I just render the above link bolded and black (not blue) and maintain the non-clickable nature?
UPDATE (solution using UITextView's linkTextAttributes)
self.testTextView.editable = NO;
self.testTextView.selectable = YES;
self.testTextView.userInteractionEnabled = NO; // workaround to disable link - CAUTION: it also disables scrolling of UITextView content
self.testTextView.dataDetectorTypes = UIDataDetectorTypeLink;
self.testTextView.linkTextAttributes = #{NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0f], // NOT WORKING !?
NSForegroundColorAttributeName : [UIColor redColor]};
...
self.testTextView.text = #"Lorem ipsum http://www.apple.com Lorem ipsum";
As you can see in comments, I wasn't able to set new font to linkTextAttributes, though the colour attribute was working as expected.
If you can get away with colour attribute or some other text attribute to style your URLs and you don't have to worry about disabled UITextView scrolling, then this may be your solution.
PREVIOUS (alternative solution)
If you're using Storyboard/xib then make sure you've deselected Detection -> Links for your UITextView. You can make your link bold by setting its container font to some bold typeface. If you want to support different text/font styles in one string object then you should really look for NSAttributedString or NSMutableAttributedString.
See: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSAttributedString_Class/Reference/Reference.html.
Example:
UIFont *linkFont = [UIFont fontWithName:#"SomeBoldTypeface" size:12];
NSString *link = #"food item - more item stuff";
NSMutableAttributedString *someString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"is my body for the note %#; let me ad", link]];
[someString addAttribute:NSFontAttributeName value:linkFont range:NSMakeRange(24, link.length)];
UITextView *textView = [[UITextView alloc] init];
textView.attributedText = someString;
...
I have a text view in which text is displayed like this:
how are you?
Fine
Now if i set font for text view, then the same font is displayed for the two lines(ques and answer), however i want question to be displayed in one font and answer in some other font. How can i do this?
I set font like this:
textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 300, 440)];
textView.layer.borderColor = [UIColor blackColor].CGColor;
[textView setFont:[UIFont fontWithName:#"TimesNewRomanPS-ItalicMT" size:14]];
textView.layer.borderWidth = 1.0;
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
Thanks in advance!!
From the UITextView class reference:
In iOS 6 and later, this class supports multiple text styles through
use of the attributedText property. (Styled text is not supported in
earlier versions of iOS.) Setting a value for this property causes the
text view to use the style information provided in the attributed
string. You can still use the font, textColor, and textAlignment
properties to set style attributes, but those properties apply to all
of the text in the text view.
This class does not support multiple styles for text. The font, color,
and text alignment attributes you specify always apply to the entire
contents of the text view. To display more complex styling in your
application, you need to use a UIWebView object and render your
content using HTML.
So you cannot have two on the same page for iOS 5 or less because it is not supported. Just use a webview and an HTML file. for iOS6 maybe you can try using attributedText property of UITextView. This is available under iOS 6. Never tried it though.
Or have 2 different UITextView's (its ugly but thats what it is).
I'm guessing you wanted to create a chatroom like app?
if so, I recommend make it a UITableView. And then make different Cells to match different styles.
You can use attributed strings to achieve this, for example:
NSMutableAttributedString *para1 = [[NSMutableAttributedString alloc] initWithString:#"How are you?"];
NSMutableAttributedString *para2 = [[NSMutableAttributedString alloc] initWithString:#"\nFine"];
[para2 setAttributes:#{ NSForegroundColorAttributeName : [UIColor blueColor]} range:NSMakeRange(0, para2.length)];
[para1 insertAttributedString:para2 atIndex:para1.length];
self.textLabel.attributedText = para1;
Or with a single attributed string:
NSMutableAttributedString *para1 = [[NSMutableAttributedString alloc] initWithString:#"How are you?\nFine"];
// Get the range of the last line in the string
__block NSRange range;
[para1.mutableString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
range = [para1.mutableString rangeOfString:line];
}];
[para1 setAttributes:#{ NSForegroundColorAttributeName : [UIColor blueColor] } range:range];
self.textLabel.attributedText = para1;
Both examples result in: