How can I set a TTStyledTextLabel inside of a UITableView.
Each TTStyledTextLabel contains Some parsed HTML.
Heres what I have I realize its probably completely wrong.
TTStyledTextLabel* label = [[TTStyledTextLabel alloc] autorelease];
cell.textLabel.text = [TTStyledText textFromXHTML:tempString lineBreaks:YES URLs:YES];
App Crashes on launch. I think its because I am setting the .text property with something that is not text. However, I don't know what else to set.
The following code will do what you want. Unfortunately, however, I cannot figure out how to automatically set the height. If memory isn't an issue you could keep a seperate array of TTStyledTextLabels and reference their heights.
in your loadView:
CGRect cgRct2 = CGRectMake(0, 35, 320, 375); //define size and position of view
tblView = [[UITableView alloc] initWithFrame:cgRct2 style:UITableViewStylePlain];
tblView.dataSource = [self constructDataSource];
tblView.delegate = self;
//[tblView reloadData];
[myView addSubview:tblView];
in your class:
-(TTListDataSource *)constructDataSource {
NSLog(#"constructDataSource");
NSMutableArray * namesArray = [[NSMutableArray alloc] init];
//ADD ITEMS
[namesArray addObject:[TTStyledText textFromXHTML:[NSString stringWithString:#"some XHTML"]]];
TTListDataSource * dataSource = [[TTListDataSource alloc] init];
for (int i = 0; i < [namesArray count]; i++) {
TTStyledText * text = [namesArray objectAtIndex:i];
[dataSource.items addObject:[TTTableStyledTextItem itemWithText:text]];
}
[namesArray release];
return dataSource;
}
Related
I'm a noob to Iphone development (3rd day in Xcode) and I am trying to implement pageControl and scrollview so users can swipe between various pages. I'm using this tutorial and I can't figure out how to load/switch views from nib files as opposed to just changing the background color of a view. Any help is greatly appreciated.
MY CODE
Modification of PageControlExampleViewController.m renamed NewsClass2
// Creates the color list the first time this method is invoked. Returns one color object from the list.
+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index {
if (__pageControlColorList == nil) {
__pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor magentaColor],
[UIColor blueColor], [UIColor orangeColor], [UIColor brownColor], [UIColor grayColor], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]];
}
//Changing views instead of colors, not working
+ (UIView *)pageControlViewWithIndex:(NSUInteger)index {
if (__pageControlViewrList == nil) {
__pageControlViewrList = [[NSArray alloc] initWithObjects:[[UIView alloc] initWithNibName:#"PageView" bundle:nil], [[UIView alloc] initWithNibName:#"PageView" bundle:nil], [[UIView alloc] initWithNibName:#"PageView" bundle:nil],
[[UIView alloc] initWithNibName:#"PageView" bundle:nil], [[UIView alloc] initWithNibName:#"PageView" bundle:nil], [[UIView alloc] initWithNibName:#"PageView" bundle:nil], [[UIView alloc] initWithNibName:#"PageView" bundle:nil], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlViewList objectAtIndex:index % [__pageControlViewList count]];
}
// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
pageNumberLabel.text = [NSString stringWithFormat:#"Page %d", pageNumber + 1];
self.view.backgroundColor = [NewsClass2 pageControlColorWithIndex:pageNumber];
//Setting View Not Working
self.view = [NewsClass2 pageControlViewWithIndex:pageNumber];
}
welcome to Objective C.
First of all you need to set delegate of scroll view like
//in .h file write
#interface ViewController : UIViewController<UIScrollViewDelegate>
// in viewDidLoad
self.scrollView.delegate = self;
then write in delegate method of UIScrollView write...
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
then make an IBAction of valueChange for pageControl like .....
- (IBAction)changePage:(id)sender
{
int page = self.pageControlHelp.currentPage;
CGRect frame = self.scrollViewHelp.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[self.scrollViewHelp scrollRectToVisible:frame animated:YES];
}
And you have done .......
If you have any confusion for this please feel free to ask......
help please. I have this code that shows me images in scrollview.:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *imgNames = [[NSArray alloc] initWithObjects:#"ip1.jpg", #"ip2.jpg", #"ip3.jpg", #"ip4.jpg", #"ip5.jpg", #"ip6.jpg", #"ip7.jpg", #"ip8.jpg", #"ip9.jpg", #"ip10.jpg",#"ip11.jpg",#"ip12.jpg",#"ip13.jpg",#"ip14.jpg",#"ip15.jpg",#"ip16.jpg",#"ip17.jpg",#"ip18.jpg",#"ip19.jpg",#"ip20.jpg",#"ip21.jpg", nil];
// Setup the array of UIImageViews
NSMutableArray *imgArray = [[NSMutableArray alloc] init];
UIImageView *tempImageView;
for(NSString *name in imgNames) {
tempImageView = [[UIImageView alloc] init];
tempImageView.contentMode = UIViewContentModeScaleAspectFill;
tempImageView.image = [UIImage imageNamed:name];
[imgArray addObject:tempImageView];
}
CGSize pageSize = scrollViewBack.frame.size; // scrollView is an IBOutlet for our UIScrollView
NSUInteger page = 0;
for(UIView *view in imgArray) {
[scrollViewBack addSubview:view];
// This is the important line
view.frame = CGRectMake(pageSize.width * page++ + 40, 0, pageSize.width - 80, pageSize.height);
}
scrollViewBack.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);
}
Now, I want a UILabel, that will show me, Image name, when I will scroll. Help me please, I can't implement that. Thanks a lot.
In your second for loop you can acces to the indexes of the objects of imgArray and imgNames, so try this:
int idx = [imgArray indexOfObject:view];
NSString *strName = [imgNames objectAtIndex:idx];
//create the label
label.text=strName;
//add the label in the scrollView
If you want to show the label just when the new image is showed, keep the two arrays as iVars and use UIPageControl to track in wich page of the scrollview you are. (Sample code).
In your loop you could do something like this. Read the apple docs. Search google for how to make a label. Its not hard to learn this stuff if you just try.
for(NSString *name in imgNames) {
// ....
UILabel *label = [[UILabel alloc] initWithFrame:WHEREYOUWANTIT];
[label setText:name];
}
So I have class called CSImageViews (subclass of uiview), that is essentially a row of UIImageViews enclosed in a single subclassed UIView.
I've added a custom init method like so (node/yaml contains uiimage name data):
- (id)initWithFrame:(CGRect)frame withNode:(NSDictionary *)node
{
self = [super initWithFrame:frame];
if (self) {
_node = node;
_imageViewYAML = [node objectForKey:#"items"];
_imageViews = [self getImageViewsForItems:_imageViewYAML];
}
return self;
}
And my getImageViewsforItems is like so (adds them all to subview):
-(NSArray *)getImageViewsForItems:(NSArray *)items
{
NSMutableArray *ivs = [NSMutableArray arrayWithCapacity:[items count]];
for (int i = 0; i < [items count]; i++) {
NSDictionary *item = [items objectAtIndex:i];
NSString *type = [item objectForKey:#"type"];
NSString *name = [item objectForKey:#"name"];
UIImage *image = [UIImage imageNamed:name];
UIImageView *iv = [[UIImageView alloc] init];
iv.image = image;
iv.tag = i;
[ivs addObject:iv];
[self addSubview:iv];
}
return ivs;
}
Now when I add this custom view to my main view like this nothing happens:
CSImageViews *imageViews = [[CSImageViews alloc] initWithFrame:frame withNode:node];
[view addSubview:imageViews];
But if I add this 'csimageviews' container into a new uiview first it appears:
CSImageViews *imageViews = [[CSImageViews alloc] initWithFrame:frame withNode:node];
UIView *v = [[UIView alloc] initWithFrame:frame];
[v addSubview:imageViews];
[view addSubview:v];
thoughts?
Stupid error by me. In this particular subclassed UIView I have a method that was called every time its parent view controller was shown. This method actually removed all subviews from the view... so I wasn't able to see the uiimageviews when I rendered it in the initial view... doh.
I have 2 UIpicker view in my app and I want the pickers to show the last chosen row after i saved my data. I am using this selectRow:inComponent:animated: method, but where do I exactly call this method?
in viewDidLoad:
if (self.pickerView == nil) {
self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self;
self.pickerView.showsSelectionIndicator = YES;
in pickerView:viewForRow:forComponent:reusingView:
if (component == 0)
{
for (int j =20; j<=200; j++)
{
NSString* myString = [NSString stringWithFormat:#"%d",j];
[myArray addObject:myString];
}
UILabel *weightLabel0 = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 100, 32)];
Label0.text = [myArray objectAtIndex:row];
Label0.textAlignment = UITextAlignmentLeft;
Label0.backgroundColor = [UIColor clearColor];
[rowView insertSubview:weightLabel0 atIndex:1];
return rowView;
}
and i have a method here:
-(void) refreshPicker
{
NSArray* mArray = [self.myTextField.text componentsSeparatedByString:#"."];
NSNumber* str1 = [mArray objectAtIndex:0];
int selectRow1 = [str1 intValue]-20;
NSNumber* str2 = [mArray objectAtIndex:1];
int selectRow = [str2 intValue];
if (selectRow == 0) {
int selectRow2 = 0;
[self.pickerView selectRow:selectRow2 inComponent:1 animated:NO];
}
else if (selectRow == 5) {
int selectRow2 = 1;
[self.pickerView selectRow:selectRow2 inComponent:1 animated:NO];
}
[self.pickerView selectRow:selectRow1 inComponent:0 animated:NO];
[pickerView_ reloadAllComponents];
}
how should i put this together? if i call this method at, for instance, viewDidLoad, nothing happens. help please =)
I have came up with an idea. since my pickers only create the items when the textfield is being called, i.e. becomes first responder, hence, i call my refreshPicker method inside textFieldDidBeginEditing. and don't forget to set the textfield delegate to self. =) to those who encounters my problem, cheers.
I have problem with can't set new text in UITextView .I already link up the IBOutlet to textView in interface builder. Here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Detail";
[self setupTextView];
}
- (void) setupTextView
{
self.textDescription = [[[UITextView alloc] init] autorelease];
NSString *foo = #"blah blah blah";
textDescription.text = foo; //I try this and it didn't work
//here didn't work too.
//textDescription.text = [textDescription.text stringByAppendingString:foo];
/*Here is something for dynamic textview*/
CGRect frame;
frame = textDescription.frame;
frame.size.height = [textDescription contentSize].height;
textDescription.frame = frame;
/**/
}
Thank you for anu help :)
self.textDescription = [[[UITextView alloc] init] autorelease];
I think this is the problem. If textDescription is an IBOutlet you don't need to allocate it. Just remove this allocation.