I am using UITextView with textContainer property and attributed string...
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
But this textview became unSelectable.
I tried to subClass UITextView and return YES to canBecomeFirstResponder method but it still unSelectable..
how to fix this problem ?
This is Part of my code
NSString * decodedHtmlString = [[contentString stringByDecodingHTMLEntities] stringWithNewLinesAsBRs];
NSString * plainText = [decodedHtmlString stringByConvertingHTMLToPlainText];
NSData * dataString = [decodedHtmlString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
NSDictionary * attributedOptions = [NSDictionary dictionaryWithObjectsAndKeys:
NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute
, nil];
NSAttributedString * attributesString = [[NSAttributedString alloc] initWithData:dataString
options:attributedOptions
documentAttributes:nil
error:nil];
/////direction
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentRight;
NSMutableAttributedString * mutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:attributesString];
[mutableAttrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial" size:29.0] range:NSMakeRange(0, plainText.length)];
[mutableAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, plainText.length)];
NSTextStorage * textStorage = [[NSTextStorage alloc] initWithAttributedString:mutableAttrString];
NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSUInteger lastRenderedGlyph = 0;
CGFloat currentXOffset = 0;
while (lastRenderedGlyph < layoutManager.numberOfGlyphs) {
CGRect textViewFrame = CGRectMake(currentXOffset, 0, 500, 700);
CGSize columnSize = textViewFrame.size;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
[layoutManager addTextContainer:textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
[textView setSelectable:YES];
textView.scrollEnabled = NO;
currentXOffset += CGRectGetWidth(textViewFrame);
[self.scrollView addSubView:textView];
lastRenderedGlyph = NSMaxRange([layoutManager glyphRangeForTextContainer:textContainer]);
}
It seems that UITextView can't be selectable with using textContainer. So, as workaround we can retrieve attributedString from range of textContainer and then set it to the attributedText property of our UITextView.
NSAttributedString * subAttributedString = [mutableAttrString attributedSubstringFromRange:[layoutManager glyphRangeForTextContainer:textContainer]];
[textView setAttributedText: subAttributedString];
then the selection is working.
Try this
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:Size];
// create the UITextView for this column
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
Related
NSMutableAttributedString *attrString = ({
// words
NSAttributedString *leftString = [[NSAttributedString alloc] initWithString:#"好" attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:10]}];
// image
NSAttributedString *attachString = ({
UIImage *image = [UIImage imageNamed:#"项目标签"];
NSTextAttachment *attach = [NSTextAttachment new];
attach.image = image;
[NSAttributedString attributedStringWithAttachment:attach];
});
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithAttributedString:leftString];
[attrString appendAttributedString:attachString];
// set line-height
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.maximumLineHeight = 22.5;
style.minimumLineHeight = 22.5;
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attrString.length)];
// set baselineOffset
[attrString addAttribute:NSBaselineOffsetAttributeName value:#0 range:NSMakeRange(0, attrString.length)];
attrString;
});
If there is some words before the image attachment, the baselineOffset of image's attributedString can not be set to 0.
here is the demo project
I solved it myself.
Just set NSTextAttachment's bound.origin.y to font's descender.
My code to justify label text is given below:-
-(void)justifyToLabel:(UILabel*)label withStr:(NSString*)str{
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;
NSDictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: str attributes: attributes];
label.attributedText = attributedString;
}
I don't know that why its not working. Please give a best solution.
Thanks
Try the below code:
You have not assigned any value to firstLineHeadIndent property, that's why it is not working.
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified; //justified text
paragraphStyles.firstLineHeadIndent = 1.0; //must have a value to make it work
NSDictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];
label.attributedText = attributedString;
I am trying to create a UILabel where some of the text is aligned to the right and some of the text is aligned to the left. It is similar to the UITableViewCell with the small arrow:
I am trying to do it with NSAttributedString , but can't figure out what is the correct way to tackle this.
Here is some code which isn't working. It is aligned to the right.
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:#"Label >"];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentLeft;
[att addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, #"Label".length)];
NSMutableParagraphStyle *rightParagraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentRight;
[att addAttribute:NSParagraphStyleAttributeName value:rightParagraph range:NSMakeRange(5, 1)];
You can use NSAttributedString to achieve your requirements, but it will be much better and cleaner approach to use two UILabels instead.
Use 2 labels.Assign the needed TextAlignment property to them. And after setting label text value, write this line :
[textLabel sizeToFit];
Though sizes of the labels varies it will set to minimum size. and will avoid text overlap.
I did it before with that code, Hope it also working for you.
NSString* alphaString = #“some text”;
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]
initWithString:alphaString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue" size:13], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil]];
NSString * betaString = #“some other text”;
NSMutableParagraphStyle* paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle2.alignment = NSTextAlignmentRight;
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:betaString attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue" size:13], NSFontAttributeName, paragraphStyle2, NSParagraphStyleAttributeName, nil]]];
yourLabel.attributedText = attributedString;
I am creating a UIScrollView and setting the attributedText. For some reason the text was not appearing...until I actually scrolled down. It seems the text is being aligned to the bottom of the UIScrollView, and I have no idea why?!
NSString *description = NSLocalizedString(#"forgot.description", nil);
NSMutableAttributedString *paragraph = [[NSMutableAttributedString alloc] initWithString:description attributes:#{}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:4];
[paragraph addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [paragraph length])];
[paragraph addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, [paragraph length])];
[paragraph addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:43/255.0f green:47/255.0f blue:61/255.0f alpha:1.0f] range:NSMakeRange(0, [paragraph length])];
_descriptionTextView = [[UITextView alloc] init];
_descriptionTextView.backgroundColor = [UIColor whiteColor];
_descriptionTextView.attributedText = paragraph;
_descriptionTextView.userInteractionEnabled = NO;
_descriptionTextView.scrollEnabled = NO;
_descriptionTextView.clipsToBounds = NO;
CGFloat const ComponentWidth = 280.0;
CGFloat const ComponentHeight = 40.0;
self.descriptionTextView.frame = CGRectIntegral(CGRectMake(x, y, ComponentWidth, ComponentHeight));
[self.view addSubview:self.descriptionTextView];
I think this has to do with the navigationBar when I set it to not hidden
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:NO];
[super viewWillAppear:animated];
}
I have a multiple lines UILabel with attributed text.
All the lines in the text are of the same font, but each line is of a different font size.
I'm trying to achieve the exact same vertical space between each line.
However what is being displayed has variable spaces. It is as if something is adding a vertical margin to the font based on the font size.
CGFloat y = 0;
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:#""];
NSArray *linesArray = [NSArray arrayWithObjects:#"One I\n",
#"Two I\n",
#"Three I\n",
#"Four I\n",
#"Five I\n", nil];
CGFloat fontSize = 10.0;
for(NSString *line in linesArray) {
NSMutableAttributedString *attributedLine = [[NSMutableAttributedString alloc] initWithString:line];
NSInteger stringLength=[line length];
[attributedLine addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"TimesNewRomanPSMT" size:fontSize]
range:NSMakeRange(0, stringLength)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 0.0f;
paragraphStyle.alignment = NSTextAlignmentRight;
[attributedLine addAttributes:#{ NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, stringLength)];
[attString appendAttributedString:attributedLine];
fontSize += 10.0;
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
label.attributedText = attString;
[label sizeToFit];
CGRect newFrame = label.frame;
newFrame.size.width = self.view.frame.size.width - 40;
newFrame.origin.y = y;
newFrame.origin.x = 0;
label.frame = newFrame;
[self.view addSubview:label];
Any suggestions on the code I should use in order for it to display no space at all between each line of text?
I have been doing something similar, so maybe you could try something like this (typed in browser, watch out!):
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment: NSTextAlignmentRight];
[style setLineSpacing:0];
for(NSString *line in linesArray) {
NSMutableParagraphStyle *subStyle = [style mutableCopy];
[subStyle setMaximumLineHeight:10]; // play around with this value <-----
NSDictionary *attributes =
#{
NSFontAttributeName : [UIFont fontWithName:#"TimesNewRomanPSMT" size:fontSize],
NSParagraphStyleAttributeName : paragraphStyle,
};
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:line attributes: attributes]];
fontSize += 10.0;
}