TTStyledTextLabel text alignment not works when transform rotation is used - iphone

I am having a problem with Three20's TTSTyledTextLabel. i have specified the view like this
//iphone properties
htmlSubtitle_ = [[TTStyledTextLabel alloc] init];
htmlSubtitle_.font = [UIFont systemFontOfSize:14];
htmlSubtitle_.contentInset = UIEdgeInsetsMake(2, 2, 2, 2);
htmlSubtitle_.backgroundColor = [UIColor clearColor];
htmlSubtitle_.textColor = [UIColor whiteColor];
htmlSubtitle_.textAlignment = UITextAlignmentCenter;
htmlSubtitle_.userInteractionEnabled = NO;
htmlSubtitle_.text = [TTStyledText textFromXHTML:#"<p><p>I dont know where this\n is going<br/> but it is ok :D</p></p>" lineBreaks:YES URLs:YES];;
htmlSubtitle_.frame = CGRectMake(0, 390, 200, 90);
htmlSubtitle_.backgroundColor = [UIColor greenColor];
[self.view addSubview:htmlSubtitle_];
it looks like this :
And when i rotate the screen i have written this code :
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
htmlSubtitle_.frame = CGRectMake(0, 0, 90, 200);
htmlSubtitle_.transform = CGAffineTransformMakeRotation(M_PI / 2);
htmlSubtitle_.backgroundColor = [UIColor greenColor];
}
and it looks like this :
see the text alignment, the alignment has been changed to left instead of remaining in center. is somebody else having this problem? or am i doing something wrong?

I have used UIWebView to show the HTML Styled Text instead of using this TTStyledLabel crap ... Thanx everybody.

Related

Sizing UITextView to its content isn't working

Here's the code for a UITextView that I want to size to the height of its content.
If I write the textView.frame height explicitly like:
textView.frame = CGRectMake(100, 12, 320, 458);
the textView sizes to it's content as expected.
If, however, I write it like the following. It doesn't even display although the NSLog statement says that there's a value to textView.contentSize.height
UITextView *textView = [[UITextView alloc] init];
textView.layer.borderWidth = 5.0f;
textView.layer.borderColor = [[UIColor redColor] CGColor];
textView.text = [item objectForKey:#"description"];
textView.frame = CGRectMake(100, 12, 320, textView.contentSize.height);
NSLog(#"%f textviewcontnet size", textView.contentSize.height);
textView.editable = NO;
[self.view addSubview:textView];
When I log the output of:
NSLog(#"%f textviewcontent size", textView.contentSize.height);
I get "458.000000 textviewcontent size"
thanks for any help
I'd suggest trying:
UITextView *textView = [[UITextView alloc] init];
textView.layer.borderWidth = 5.0f;
textView.layer.borderColor = [[UIColor redColor] CGColor];
textView.text = [item objectForKey:#"description"];
textView.frame = CGRectMake(100, 12, 320, 458);
textView.editable = NO;
[self.view addSubview:textView];
textView.frame = CGRectMake(100, 12, 320, textView.contentSize.height);
I've heard that textView.contentSize.height doesn't work until it's been added to a view (though that's not my experience). More importantly, I don't know how it would interpret textView.contentSize.height if it doesn't yet know what the width of the control is. So go ahead, set the initial frame, do addSubview and then readjust the size based upon textView.contentSize.height.
Quickly copied out of one of my projects:
AppDelegate *appDelegate;
CGSize textSize1, textSize2;
appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
textSize1 = [self.subjectLabel.text sizeWithFont:[appDelegate fontNormal] constrainedToSize:CGSizeMake(300.0f, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
self.subjectLabel.frame = CGRectMake(10, 5, 300, textSize1.height);
textSize2 = [self.descriptionLabel.text sizeWithFont:[appDelegate fontNormal] constrainedToSize:CGSizeMake(300.0f, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
self.descriptionLabel.frame = CGRectMake(10, 5 + textSize1.height + 5, 300, textSize2.height);
[appDelegate fontNormal] just returns a UIFont object, the one that I am using for all "normal" text items. Don't worry about that too much. But it is important that you use the same font that is used for the text view too.
My example is a bit easier because it is a UILable. That works with a text view too but you will have to consider the insects. Easy solution, just substract some "fuzzy offset" from the width compared to the frame width of your text view.

Jerky scroll with UIButton.layer.shadowColor

I got a UITableView header with lots of buttons.
I use this to create a shadow on the buttons:
Buttona.layer.shadowColor = [UIColor blackColor].CGColor;
Buttona.layer.shadowOpacity = 0.7f;
Buttona.layer.shadowOffset = CGSizeMake(10.0f, 10.0f);
Buttona.layer.shadowRadius = 7.0f;
Buttona.layer.masksToBounds = NO;
Buttonb.layer.shadowColor = [UIColor blackColor].CGColor;
Buttonb.layer.shadowOpacity = 0.7f;
Buttonb.layer.shadowOffset = CGSizeMake(2.0f, 3.0f);
Buttonb.layer.shadowRadius = 2.0f;
Buttonb.layer.masksToBounds = NO;
... and so on for Buttonc to Buttonf.
The scroll on the table becomes jerky with all theses buttons.
Should I get rid of the effect and just Photoshop the buttons with the shadow, or am I doing something wrong here?
Thanks in advance.
I had the same problem and the solution is
[view.layer setShadowPath:[[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)] CGPath]];

Text appears after hitting the return key in a UITextField

I have a custom class to create a textview with lines like the Notes app from Apple.
This is how the class looks:
NoteView.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface NoteView : UITextView <UITextViewDelegate> {
}
#end
NoteView.m
#import "NoteView.h"
#implementation NoteView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
self.font = [UIFont fontWithName:#"Marker Felt" size:20];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
//Set the line offset from the baseline.
CGFloat baselineOffset = 6.0f;
//Iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
#end
I add the view as a subview. Everything works fine, but the text only apears after I hit the return key. I can see the flashing cursor, when I type I can see the cursor moving, but the text is gone... After hitting the return key, the text becomes visible and after that everything works fine. Even when I select all the text, remove it and start typing the text is visible.
This is how the textarea looks like:
How to solve this?
Thanks in advance!
UPDATE:
This is how I alloc the view:
annotateText = [[NoteView alloc] initWithFrame:CGRectMake(85.0, 168.0, 600.0, 567.0)];
annotateText.backgroundColor = [UIColor clearColor];
[annotateText setText:[annotationNotes objectAtIndex:0]];
[paperAnnotationView addSubview:annotateText];
I know this question is pretty old. I ran into the same issue and was looking for a solution. So I thought I would post the answer here, just in case, if any one comes and looks for it. After pulling my hair for hours, I figured out that the text view wont display the text in the very first line (unless we press enter/return or scroll the text) when the bounds of the textview is off the screen. Even when the parent view is off the screen, I ran into the same issue.
Here is the code that I was working on.
self.captionView = [[[UIImageView alloc] initWithFrame:CGRectMake(231, 769, 563, 643)] autorelease]; //63
self.captionView.image = [UIImage imageNamed:#"ekp006_preview_fbsharecontainer"];
self.captionView.userInteractionEnabled = YES;
//self.captionView.layer.contents = (id)[UIImage imageNamed:#"ekp006_preview_fbsharecontainer"].CGImage;
UIButton *cancelButton = [[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 73, 34)] autorelease];
[cancelButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_cancel"] forState:UIControlStateNormal];
[cancelButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_cancel_down"] forState:UIControlStateHighlighted];
[cancelButton addTarget:self action:#selector(cancelFacebookShare) forControlEvents:UIControlEventTouchUpInside];
[self.captionView addSubview:cancelButton];
UIButton *shareButton = [[[UIButton alloc] initWithFrame:CGRectMake(480, 10, 73, 34)] autorelease];
[shareButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_share"] forState:UIControlStateNormal];
[shareButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_share_down"] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:#selector(facebookshare) forControlEvents:UIControlEventTouchUpInside];
[self.captionView addSubview:shareButton];
self.captionTextView = [[[UITextView alloc] initWithFrame:CGRectMake(20, 60, 523, 100)] autorelease];
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.delegate = self;
self.captionTextView.layer.shadowRadius = 5.0;
self.captionTextView.layer.shadowColor = [UIColor blackColor].CGColor;
self.captionTextView.layer.shadowOpacity = 0.8;
self.captionTextView.layer.borderColor = [UIColor blackColor].CGColor;
self.captionTextView.layer.borderWidth = 0.75;
self.captionTextView.layer.cornerRadius = 5.0f;
self.captionTextView.font = [UIFont fontWithName:#"VAGRoundedBlackSSi" size:18];
[self.captionView addSubview:self.captionTextView];
[[self topMostViewController].view addSubview:self.captionView];
If you see closely, the parent view for me (the caption view) was 769 in y origin and I made this explicit, so that I can make the view slide from bottom to top using UIView animations. In order to solve this, I had to take the alternate path of making the parent view frame as CGRectMake(231,63,563,643) and hide the entire view. And to present it I used something like this.
- (void) presentCaptionCaptureScreen
{
// The code which works
[AnimationEffects bounceAnimationForView:self.captionView afterTimeInterval:0];
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.text = #"Enter your caption for the photo";
// The code which didn't work
/*
[UIView animateWithDuration:0.7 animations:^
{
self.captionTextView.text = #"";
self.captionView.frame = CGRectMake(231, 63, 563, 643);
} completion:^(BOOL finished)
{
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.text = #"Enter your caption for the photo";
}];
*/
}
It worked for me!

UILabel getting displayed differently on 3rd and 4th gen iPods

I have a UILabel which behaves differently on 3rd & 4th Gen iPods.
My UILabel is:
<UILabel: 0x881e80; frame = (10 76; 70 28); text = 'Produits Indisponibles'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x863fe0>>
Now, on 4th gen iPod it works fine and text wraps into 2 lines properly but on 3rd gen iPod text wraps but second word (Indisponibles) disappears. What could be the reason?
If I increase the frame width by 1 more pixel, it works fine. Here is my code:
self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.titleLabel.text = #"Produits Indisponibles";
self.titleLabel.isAccessibilityElement = NO;
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.opaque = NO;
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.numberOfLines = 2;
self.titleLabel.highlightedTextColor = [UIColor whiteColor];
self.titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
self.titleLabel.textAlignment = UITextAlignmentCenter;
self.titleLabel.font = [UIFont boldSystemFontOfSize:11];
CGSize aLabelSize = CGSizeMake(self.frame.size.width - 20, 28);
CGSize aStringSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:aLabelSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat theYOrigin = self.bounds.size.height - 34;
if (aStringSize.height < 15) {
theYOrigin = theYOrigin + 14;
}
self.titleLabel.frame = CGRectMake(10, theYOrigin, aLabelSize.width, aStringSize.height);
[self addSubview:self.titleLabel];
Retina displays use Helvetica Neue as the system font, while older models use normal Helvetica. It's probably related to the different font metrics. Hard-code a font you know is available on both to see if they display consistently.
I do not see the reason but when I increased the width of the frame by 2 more pixels it worked. Anyone know the reason for this will be enlightening.

UILabel not aligning perfectly center when wrapping

See image for example: http://img25.imageshack.us/img25/6996/90754687.png
The grey background indicates the size of the UILabel frame.
For some reason, the first line of wrapped text doesn't seem to always center, even though I'm using UITextAlignmentCenter.
Here's the code I use to set up my labels:
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.font = [UIFont boldSystemFontOfSize:fontHeight];
titleLabel.backgroundColor = [UIColor grayColor];
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeMiddleTruncation;
NSString * title = file.name;
CGSize maximumSize = CGSizeMake(thumbnailWidth+4,fontHeight * 3);
UIFont * font = [UIFont boldSystemFontOfSize:12];
CGSize stringSize = [title sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:titleLabel.lineBreakMode];
CGRect stringFrame = CGRectMake(0, thumbnailHeight + thumbnailPadding, thumbnailWidth + 4, stringSize.height);
titleLabel.text = title;
titleLabel.frame = stringFrame;
titleLabel.textAlignment = UITextAlignmentCenter;
Is that because there are no spaces in the text? In IB it appears to react just like your getting, if you have no spaces. Setting the line break mode to character wrap tends to center the second lines to the first, but that may not be entirely what you want either.