How to get CALayer to be clear? - ios

I have this code that returns a CATextLayer to place over a video and it is returning blurry text. I have searched online and most people recommended adding textLayer.contentsScale but this did not work for me.
- (CATextLayer *)buildTextLayerWithText:(NSString *)text atPosition:(WatermarkPosition)position forVideoSize:(CGSize)videoSize
{
CATextLayer *textLayer = [[CATextLayer alloc] init];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
CGFloat fontSize = videoSize.height / 20.0;
[attributes setObject:(__bridge id)(__bridge CFTypeRef)([ChannelsInterface mediumFontOfSize:fontSize]) forKey:(NSString*)kCTFontAttributeName];
[attributes setObject:(__bridge id)[[UIColor whiteColor] CGColor] forKey:(NSString*)kCTForegroundColorAttributeName];
[attributes setObject:[NSNumber numberWithFloat:fontSize] forKey:(NSString*)kCTFontSizeAttribute];;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
textLayer.string = attributedString;
CGSize textLayerSize = [text sizeWithAttributes:attributes];
textLayer.contentsScale = [UIScreen mainScreen].scale;
[textLayer setFrame:getFrameForPosition(textLayerSize, videoSize, position)];
textLayer.shadowColor = [UIColor blackColor].CGColor;
textLayer.shadowRadius = 1.0f;
textLayer.shadowOpacity = 0.5f;
textLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
[textLayer setRasterizationScale:[UIScreen mainScreen].scale];
[textLayer setShouldRasterize:YES];
return textLayer;
}

Related

How to set text on uiimage center?

UIGraphicsBeginImageContext(targetSize);
NSString *txt = #"my String";
UIColor *txtColor = [UIColor whiteColor];
UIFont *txtFont = [UIFont systemFontOfSize:30];
NSDictionary *attributes = #{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor};
[txt drawAtPoint:CGPointMake(0, 0) withAttributes:attributes];
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I don't know how to fix it to make text to Center.
//Edit
I add your device but in vain
NSMutableParagraphStyle *styleCenter = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
styleCenter.alignment = kCTCenterTextAlignment;
NSDictionary *attributes = #{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor,NSParagraphStyleAttributeName:styleCenter};
[txt drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height) withAttributes:attributes];
the text show at right not center, so weird
Try this:
UIGraphicsBeginImageContext(targetSize);
NSString *txt = #"my String";
UIColor *txtColor = [UIColor whiteColor];
UIFont *txtFont = [UIFont systemFontOfSize:30];
NSDictionary *attributes = #{NSFontAttributeName:txtFont, NSForegroundColorAttributeName:txtColor};
CGRect txtRect = [txt boundingRectWithSize:CGSizeZero
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
[txt drawAtPoint:CGPointMake(targetSize.width/2 - txtRect.size.width/2, targetSize.height/2 - txtRect.size.height/2) withAttributes:attributes];
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSMutableParagraphStyle *styleCenter = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
styleCenter.alignment = NSCenterTextAlignment;
NSDictionary *attributes = #{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor,NSParagraphStyleAttributeName:styleCenter};
[txt drawInRect:rect withAttributes:attributes];
This category on NSString will draw the centered text in rect with given below font:
#implementation NSString (Helpers)
- (void)drawCentredInRect:(CGRect)rect withFont:(nullable UIFont *)font andForegroundColor:(nullable UIColor *)foregroundColor {
NSMutableDictionary *attributes = [NSMutableDictionary new];
if (font) {
attributes[NSFontAttributeName] = font;
}
if (foregroundColor) {
attributes[NSForegroundColorAttributeName] = foregroundColor;
}
CGSize textSize = [self sizeWithAttributes:attributes];
CGPoint drawPoint = CGPointMake(
CGRectGetMidX(rect) - (int) (textSize.width / 2),
CGRectGetMidY(rect) - (int) (textSize.height / 2)
);
[self drawAtPoint:drawPoint withAttributes:attributes];
}
I tried the answer for your clever question,Now I got the solution
+(UIImage*) drawText:(NSString*)text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIGraphicsBeginImageContextWithOptions(image.size, YES, 0.0f);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
if([text respondsToSelector:#selector(drawInRect:withAttributes:)])
{
//iOS 7 or above iOS 7
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
NSDictionary *attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName];
[text drawInRect:rect withAttributes:attr];
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Please call the above method in where you pick the image like below
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imagePicked = info[UIImagePickerControllerEditedImage];
picker.delegate = self;
self.imageViewTextCenter.image = [ViewController drawText:#"WELCOME"
inImage:imagePicked
atPoint:CGPointMake(0, 0)];;
[picker dismissViewControllerAnimated:YES completion:nil];
}

UILabel updating text, but won't remove old text

EDIT: I figured out that i needed to remove subviews, once the view did disappear
[[self.scrollView subviews]makeObjectsPerformSelector:#selector(removeFromSuperview)];
My problem is that I have an UILabel which updates the text correctly, but won't remove the old text. I think maybe it's because there are 2 UILabels on top of each other, here is a picture of it:
And here is my code. I can't see where the duplicate is:
for (int i = 0; i < array1.count; i++) {
NSNumber *myNumber = [myscoretext objectAtIndex:i];
float myScore = myNumber.floatValue;
NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
float levelScore = levelNumber.floatValue;
for (int i = 0; i < array1.count; i++) {
NSNumber *myNumber = [myscoretext objectAtIndex:i];
float myScore = myNumber.floatValue;
NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
float levelScore = levelNumber.floatValue;
float progressScore = ((float)myScore/(float)levelScore);
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor clearColor];
NSArray *colorArray = [NSArray arrayWithObjects:edmeral, turqouise, orange, red, nil];
// Labeled progress views
self.labeledProgressView = [[DALabeledCircularProgressView alloc]
initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
self.labeledProgressView.roundedCorners = NO;
self.labeledProgressView.trackTintColor = [UIColor colorWithWhite:1.0f alpha:0.8];
imageLevel = [[UIImageView alloc] initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
imageLevel.backgroundColor = [UIColor clearColor];
self.Scoreint = [[UILabel alloc] initWithFrame:CGRectMake(-25.0f, 130, 195, 21)];
self.Scoreint.backgroundColor = [UIColor clearColor];
self.Scoreint.textColor = [UIColor blackColor];
[self.Scoreint setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = #"";
if (myScore == 0) {
NSString *scoreString = [NSString stringWithFormat:#"0 / %5ld", (long)levelScore];
[self.Scoreint setText:scoreString];
}
else {
NSString *scoreString = [NSString stringWithFormat:#"%5li / %5li", (long)myScore, (long)levelScore];
[self.Scoreint setText:scoreString];
}
[subview addSubview:Scoreint];
I hope some of you guys can help me out with this! :)
the other way, you should do it, it's remove the label from superView everytime, before you initialize it.
You modify the code like this,
if(self.Scoreint) {
[self.Scoreint removeFromSuperview];
}
self.Scoreint = [[UILabel alloc] initWithFrame:CGRectMake(-25.0f, 130, 195, 21)];
self.Scoreint.backgroundColor = [UIColor clearColor];
self.Scoreint.textColor = [UIColor blackColor];
[self.Scoreint setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = #"";
that happens because you instantiate the UILabel each time, You have to instantiate the it only once, outside the for loop, and then just change the title.
This might work, i haven't tried it
self.Scoreint = [[UILabel alloc] initWithFrame:CGRectMake(-25.0f, 130, 195, 21)];
self.Scoreint.backgroundColor = [UIColor clearColor];
self.Scoreint.textColor = [UIColor blackColor];
[self.Scoreint setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = #"";
for (int i = 0; i < array1.count; i++) {
NSNumber *myNumber = [myscoretext objectAtIndex:i];
float myScore = myNumber.floatValue;
NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
float levelScore = levelNumber.floatValue;
for (int i = 0; i < array1.count; i++) {
NSNumber *myNumber = [myscoretext objectAtIndex:i];
float myScore = myNumber.floatValue;
NSNumber *levelNumber = [neededscoretext objectAtIndex:i];
float levelScore = levelNumber.floatValue;
float progressScore = ((float)myScore/(float)levelScore);
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor clearColor];
NSArray *colorArray = [NSArray arrayWithObjects:edmeral, turqouise, orange, red, nil];
// Labeled progress views
self.labeledProgressView = [[DALabeledCircularProgressView alloc]
initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
self.labeledProgressView.roundedCorners = NO;
self.labeledProgressView.trackTintColor = [UIColor colorWithWhite:1.0f alpha:0.8];
imageLevel = [[UIImageView alloc] initWithFrame:CGRectMake(25.0f, 20.0f, 100.0f, 100.0f)];
imageLevel.backgroundColor = [UIColor clearColor];
[self.Scoreint setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15]];
self.Scoreint.textAlignment = NSTextAlignmentCenter;
self.Scoreint.text = #"";
if (myScore == 0) {
NSString *scoreString = [NSString stringWithFormat:#"0 / %5ld", (long)levelScore];
[self.Scoreint setText:scoreString];
}
else {
NSString *scoreString = [NSString stringWithFormat:#"%5li / %5li", (long)myScore, (long)levelScore];
[self.Scoreint setText:scoreString];
}
[subview addSubview:Scoreint];
In similar situation, the property of UIView’s visual appearance helped me. Try this:
self.Scoreint.clearsContextBeforeDrawing = true

Can I figure out the numberOfLines of NSString without creating UILabel?

I just want to know number of lines, given the font, constraints, and the text. Can I figure it out without creating a UILabel?
+ (int)numberOfLines:(NSDictionary *)data{
NSString *myString = [some string calculation];
CGSize sizeConstrain = CGSizeMake(some constrain calculation);
CGSize stringSize = [myString sizeWithFont:someFont constrainedToSize:sizeConstrain];
CGRect labelFrame = CGRectMake(0,
0,
stringSize.width,
stringSize.height + 2);
UILabel *label = [[UILabel alloc]initWithFrame:labelFrame];
label.text = myString;
return label.numberOfLines;
}
Yes
+ (int)numberOfLines:(NSDictionary *)data{
NSString *myString = [some string calculation];
CGSize sizeConstrain = CGSizeMake(some constrain calculation);
CGSize stringSize = [myString sizeWithFont:someFont constrainedToSize:sizeConstrain];
return (stringSize.height/someFont.lineHeight);
}
EDIT: I used this for UITextView and iOS7
- (CGFloat) getRowsForText:(NSString*) text{
CGFloat fixedWidth = 300;
UIFont *font = [UIFont fontWithName:#"HelveticaNeue" size:14];
NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
paragrapStyle.alignment = NSTextAlignmentLeft;
textStepAttr = [NSDictionary dictionaryWithObjectsAndKeys:
font,NSFontAttributeName,
paragrapStyle, NSParagraphStyleAttributeName,
nil];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:textStepAttr];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(fixedWidth, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return (rect.size.height / font.lineHeight) ;
}

iPhone View Size Not Correct After Rotation

I am creating an iOS app that needs to run on all device. When creating a slideshow I did not want to have an image for each device. I creating a method to generate the images. It takes a background image, main sink image, and 2 UILabels and scales accordingly. For portrait orientation on all the devices it works perfectly. On the iPad I also need landscape. When I rotate it something goes wrong because it just stretches the images. I am not sure what is happening to my view bounds.
Sorry my code is so long.
-(void)viewDidLoad{
_screenSize = self.view.bounds.size;
_slideShowImageArray = [[NSMutableArray alloc] init];
_slideShow = [[NSMutableArray alloc] init];
for (int i = 1; i<=8; i++) {
[_slideShow addObject:[self slideNumber:[NSNumber numberWithInt:i]]];
}
[self generateImages];
_SlideImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, _screenSize.width, _screenSize.height)];
[[self view]addSubview:_SlideImageView];
[self setSlideNumber:[NSNumber numberWithInt:1]];
[self begin];
}
-(void)begin{
if ([_SlideNumber intValue] <=8) {
UIImage * toImage = [_slideShowImageArray objectAtIndex:[_SlideNumber intValue]-1];
[UIView transitionWithView:self.view
duration:1.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.SlideImageView.image = toImage;
} completion:^(BOOL finished) {
sleep(2);
[self begin];
}];
int SlideNumber = [_SlideNumber intValue];
int NewSlideNumber = SlideNumber+=1;
[self setSlideNumber:[NSNumber numberWithInt:NewSlideNumber]];
}
}
-(SlideShowObj*) slideNumber:(NSNumber*)number{
SlideShowObj * slideShowOBJ = [[SlideShowObj alloc] init];
UIImageView * counterTempImageView = [[UIImageView alloc]init];
NSString * imageName = [NSString stringWithFormat:#"bg%d.png",[number intValue]];
counterTempImageView.image = [UIImage imageNamed:imageName];
[slideShowOBJ setCounterTopIMG:counterTempImageView];
UIImageView * sinkTempImageView = [[UIImageView alloc]init];
imageName = [NSString stringWithFormat:#"sink%d.png",[number intValue]];
sinkTempImageView.image = [UIImage imageNamed:imageName];
[slideShowOBJ setSinkIMG:sinkTempImageView];
NSString * textAboveSink = #"";
NSString * textBelowSink = #"";
UIColor * textAboveSinkColor = [UIColor blackColor];
UIColor * textBelowSinkColor = [UIColor blackColor];
if ([number intValue]==1) {
textAboveSink = [NSString stringWithFormat:#"8 CAPTIVATING \nCOLORS"];
textBelowSink = [NSString stringWithFormat:#"Anthracite"];
textAboveSinkColor = [UIColor whiteColor];
textBelowSinkColor = [UIColor whiteColor];
}else if ([number intValue]==2) {
textAboveSink = [NSString stringWithFormat:#"TO ENHANCE \nANY DECOR"];
textBelowSink = [NSString stringWithFormat:#"Cinder"];
textAboveSinkColor = [UIColor whiteColor];
textBelowSinkColor = [UIColor whiteColor];
}else if ([number intValue]==3) {
textAboveSink = [NSString stringWithFormat:#"REAL TOUGH,\nFOR REAL LIFE"];
textBelowSink = [NSString stringWithFormat:#"Metallic Grey"];
textAboveSinkColor = [UIColor whiteColor];
textBelowSinkColor = [UIColor whiteColor];
}else if ([number intValue]==4) {
textAboveSink = [NSString stringWithFormat:#"HYGENIC+PLUS \nKEEPS IT CLEAN"];
textBelowSink = [NSString stringWithFormat:#"Café Brown"];
textAboveSinkColor = [UIColor blackColor];
textBelowSinkColor = [UIColor blackColor];
}else if ([number intValue]==5) {
textAboveSink = [NSString stringWithFormat:#"HEAT PROOF \nACID RESISTANT"];
textBelowSink = [NSString stringWithFormat:#"Truffle"];
textAboveSinkColor = [UIColor blackColor];
textBelowSinkColor = [UIColor blackColor];
}else if ([number intValue]==6) {
textAboveSink = [NSString stringWithFormat:#"SCRATCH RESISTANT \nSTAIN RESISTANT"];
textBelowSink = [NSString stringWithFormat:#"Biscotti"];
textAboveSinkColor = [UIColor whiteColor];
textBelowSinkColor = [UIColor whiteColor];
}else if ([number intValue]==7) {
textAboveSink = [NSString stringWithFormat:#"NON-FADING \nKEEPS IT CLEAN"];
textBelowSink = [NSString stringWithFormat:#"Biscuit"];
textAboveSinkColor = [UIColor whiteColor];
textBelowSinkColor = [UIColor whiteColor];
}else if ([number intValue]==8) {
textAboveSink = [NSString stringWithFormat:#"FOR THE LIFE \nOF THE SINK"];
textBelowSink = [NSString stringWithFormat:#"White"];
textAboveSinkColor = [UIColor whiteColor];
textBelowSinkColor = [UIColor whiteColor];
}
[slideShowOBJ setTopTextColor:textAboveSinkColor];
[slideShowOBJ setBottomTextColor:textBelowSinkColor];
[slideShowOBJ setTopText:textAboveSink];
[slideShowOBJ setBottomText:textBelowSink];
return slideShowOBJ;
}
-(void)generateImages{
for (int i = 1; i<=8; i++) {
UIImage * bgImage = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]counterTopIMG]image];
//find out the width and height ratio of your image.
double ratio = bgImage.size.width / bgImage.size.height;
//determine the calculate width according the height. here height is set as mainScreen.
double calculatedWidth = ratio * _screenSize.height;
UIImageView * bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, calculatedWidth, _screenSize.height)];
[[self view] addSubview:bgImageView];
bgImageView.image = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]counterTopIMG]image];
UIImage * image = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]sinkIMG]image];
//find out the width and height ratio of your image.
double ratio1 = image.size.width / image.size.height;
//determine the calculate width according the height. here height is set as mainScreen.
double calculatedWidth1 = ratio1 * (_screenSize.height)*.5;
double calculatedheight1 = ratio1 * _screenSize.width*.5;
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_screenSize.width/2-calculatedWidth1/2, ((_screenSize.height)/2-calculatedheight1/2)*1.3, calculatedWidth1, calculatedheight1)];
[[self view] addSubview:imageView];
imageView.image = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]sinkIMG]image];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
UIFont *helvFont28 = [UIFont fontWithName:#"Helvetica" size:28.0];
UIFont *helvFont24 = [UIFont fontWithName:#"Helvetica" size:24.0];
UILabel * topText = [[UILabel alloc] initWithFrame:CGRectMake(0, (_screenSize.height)/2-(_screenSize.height)*.4, _screenSize.width, 100)];
[topText setNumberOfLines:2];
[topText setFont:helvFont28];
[topText setTextAlignment:NSTextAlignmentCenter];
[topText setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.2]];
[topText setTextColor:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]topTextColor]];
[[self view]addSubview:topText];
[topText setText:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]topText]];
UILabel * bottomText = [[UILabel alloc] initWithFrame:CGRectMake(0, (_screenSize.height)/2+(_screenSize.height)*.3, _screenSize.width, 40)];
[bottomText setFont:helvFont24];
[bottomText setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.2]];
[[self view]addSubview:bottomText];
[bottomText setText:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]bottomText]];
[bottomText setTextAlignment:NSTextAlignmentCenter];
[bottomText setTextColor:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]bottomTextColor]];
UIGraphicsBeginImageContext(_screenSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[[[self view]layer] renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(#"%f,%f",theImage.size.width, theImage.size.height);
[_slideShowImageArray addObject:theImage];
UIGraphicsEndImageContext();
[bottomText removeFromSuperview];
[topText removeFromSuperview];
[imageView removeFromSuperview];
[bgImageView removeFromSuperview];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[self reload];
}
-(void)reload{
[_SlideImageView setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, _screenSize.width, _screenSize.height)];
[_slideShow removeAllObjects];
[_slideShow removeAllObjects];
for (int i = 1; i<=8; i++) {
[_slideShow addObject:[self slideNumber:[NSNumber numberWithInt:i]]];
}
[self generateImages];
[self setSlideNumber:[NSNumber numberWithInt:1]];
[self begin];
}
The frame of your view hasn't been correctly set in viewDidLoad method, yet. It would be more accurate to move frame-size related code to viewWillAppear: method. Dimensions of the frame will be correct in viewWillAppear: or viewDidAppear: methods. Then, you may want to keep a boolean variable in order to generate images at the first appearance only.

Equal spacing between lines of NSAttributedString with multiple font sizes for a UILabel in iOS 6

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;
}

Resources