This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITextView formatting with fontname and bold
I am trying to Bold selected text of UITextView.I don't want to use UIWebView in my application.
How can I do that programmatically in UITextView????
Try -
textViewInstance.font = [UIFont boldSystemFontOfSize:14];
Edit 1: I didn't notice your statement of selected text. This just bolds the entire text. Sorry ;)
I was facing a similar issue. According to the documentation and the lack of answers from google, I doubt there's a solution as UITextView has only one format setting for all its text. It's all or nothing for UILabels and UITextView. The only way I see it, is to use UIWebView which gives you more freedom of styling individual words and text in the view. Good luck. :)
In UITextView i dont have any idea but i am done this in UILabel.
First set text in UILabel and setFrame of that label according to the text Height. After that use the regular Expression for the get frame for text which you want to make bold and add new label on that frame with bold font and text.
If you want to demo for above explanation then see the below link. It's have nice implementation.
FancyLabel_1.0
Edit: Now you can make selected text bold using "NSMutableAttributedString". See below peace of code.
NSString * string = #"<Your Full String>";
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:string];
[attStr addAttributes:#{NSFontAttributeName:[UIFont systemFontOfSize:13]} range:NSMakeRange(0, string.length)];
NSString * subString = #"<String that you want to make bold>";
[attStr addAttributes:#{NSFontAttributeName:[UIFont boldSystemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]} range:[string rangeOfString:subString]];
[<Your UILabel Object> setAttributedTitle:attStr forState:UIControlStateNormal];
Thanks,
MinuMaster
Related
I create an instance of UITextView programmatically. I want to assign some text to particular line in UITextView programmatically. Here is my code to create UITextView.
UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);
[textView setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:textView];
For example I want to add some text to particular line (at the end of line 5).
Programmatically, how is it possible to do this?
UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);
textView.text = #"xxxxxx";
...
You can't edit lines directly in the text view because the number of lines depends on the content size and the size of the font.
Check this question as a reference:
How to Read Number of lines in UITextView
You could do some calculation to determine where to insert the text, but I'm nor sure if that's the best way to go around. There is probably a better way to accomplish what you are trying to do.
you can use the following:
NSString *str=yourTextview.text;
[str stringByAppendingString:yourNewString];
Then add it to textview
yourTextView.text=str;
check it and let me know if any clarification you need
I have a long text containing line breaks and paragraph breaks; and I want to be able to create pages of the text.
The problem that I am facing is that I am unable to separate out the text that is visible on the page; from the text that goes beyond bounds of the page.
I was able to get the label size that can display all the text via using sizeWithFont:
CGSize expectedLabelSize = [string sizeWithFont:_lbl.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
Now, I wants to be able to separate out the visible and non visible text and create pages on the basis of it.
Any kind of help will be very much appreciated !
Check out this question here:
How to split long NSString into pages
I'm currently struggling with the need to display strikethrough text in many UITableViewCells. Something that written in HTML would looke like
<strike>€99</strike> save 50% => now €49
I don't want to use a UIWebView just for a single line of text, especially that it's used in small UITableViewCells. I know there are reusable cells and all, but I'd like to keep things the more memory-efficient way possible.
So... I'm using NSAttributedStrings, with the help of AliSoftware's UILabel-replacement OHAttributedLabel. The fact that it's only available starting with iOS 4.0 is no problem, as we use all kinds of stuff only 4.0-compatible.
I can manage to create the attributed string, it displays text in the OHAttributedLabel, OK, that's cool. But what I can't achieve is setting the "strikeout", or "strikethrough" attribute.
Basically I go like this:
NSString *price = [NSString stringWithFormat:#"%01.2f €", product.price];
NSString *rate = [NSString stringWithFormat:#" -%01.0f%%", product.reductionRate];
NSMutableAttributedString *s = [NSMutableAttributedString attributedStringWithString:price];
[s addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSRangeFromString(price)];
[attributedLabel setAttributedText:s];
But here, the three NS* constants are undefined. I've imported CoreText.h, Foundation/NSAttributedString.h, to no avail. I've seen somewhere on the web that
NSStrikethroughStyleAttributeName = #"NSStrikethroughStyleAttributeName", and that NSUnderlinePatternSolid = 0 and NSUnderlineStyleSingle = 1, but hard-coding these values don't give anything.
One thing I got with auto-completion are the equivalent kCT...* constants, but there are kCTUnderlineStyleAttributeName, kCTStrokeWidthAttributeName, ... but no mention of kCTStrikethrough_anything.
What should I do to display that *$|#!# piece of strike-through text ?
With iOS 6 you can use NSStrikethroughStyleAttributeName
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];
While it may seem out of place, the numberWithInt value is correct as NSUnderlineStyleSingle.
A simpler approach might be two labels, using the answer to this question - Pixel Width of the text in a UILabel - to strikeout the text in one of the labels.
How can I get a UITableView to automatically wrap text which is longer than the width of the cell?
You need to dynamically determine the height of the cell, and set your label so that it autosizes itself. There's a fairly comprehensive explanation here: http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
Set the "setNumberOfLines" property of the label to wrap the text to required number of lines. If you don't want the .... at the end of the text if it is too long then
label.lineBreakMode = UILineBreakModeWordWrap;
or if you want to show the ... after the text then don't use the above code.
All the best.
See the options for the lineBreakMode of the UILabel inside the UITableViewCell here.
hth
–f
Does anyone know how to create a text field that has a UIImage background and does word-wrapping?
It appears that word-wrapping is not available on UITextField, while a background image is not available for UITextView! Also, it should change the size of the control or at least alert that the number of lines changed so that a delegate could change its size..
An example of such control is the input field for messages in the SMS application.
Edit: the code should also get the text field to always show the text being edited. Apparently, when UITextView changes size while editing it somehow loses focus on the current text.
Thanks ahead!
Aviad.
Word-wrapping is not available on UITextField because UITextField only supports single-line text.
Use a UITextView. Make textView.backgroundColor = [UIColor clearColor]. Add the UIImage first, then the UITextView on top of it. Make sure the sizes are correct, and you should be fine.
As for changing the size of the UITextView, in your UITextViewDelegate, do something like this:
- (void)textViewDidChange:(UITextView *)textView
{
NSString *s = textView.text;
CGSize testSize = CGSizeMake(textView.frame.size.width, NSIntegerMax);
CGSize neededSize = [s sizeWithFont: textView.font constrainedToSize: testSize lineBreakMode: UILineBreakModeWordWrap]; // or UILineBreakModeCharacterWrap?
if (neededSize.height > testSize.height)
{
// grow textView
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, neededSize.width, neededSize.height);
// then adjust the image size -- something like this
imageView.frame = textView.frame
}
}
Eventually I wrote my own code, which worked to some degree with a lot of workarounds (changing the focus for example, resizing when removing all the text again, etc). If anyone's interested, I'll post the code.
In other news, just today I saw in a different question around here something called IFVerticallyExpandingTextField. The name was promising, the code is here, and I'll give it a look. Anyone with a similar problem, you might want to see this too.
My mistake, I didn't read it carefully enough: IFVerticallyExpandingTextField is for the Mac, not the iPhone.