網頁

顯示具有 iOS 標籤的文章。 顯示所有文章
顯示具有 iOS 標籤的文章。 顯示所有文章

2013年2月7日 星期四

pushViewController


#import "Test.h"
[self goTestPage];

- (void) goTestPage
{
    Test *TestPage = [self.storyboard instantiateViewControllerWithIdentifier:@"idTest"];
    [self.navigationController pushViewController:TestPage animated: YES];
}

2012年6月19日 星期二

UILabel TTTAttributedLabel

iOS學習_UILabel顯示兩種以上的顏色


iOS - Using TTTAttributedLabel to set two color text


[][]TTTAttributedLabelをつかってみた!



1. 下載程式碼

2. 將TTTAttributedLabel.h 與 TTTAttributedLabel.m加到你的專案中。

#import "TTTAttributedLabel.h"


3. 有開ARC 使用第三方的函數庫時 在Targets設定中的Build Phases選擇TTTAttributedLabel.m,按下Enter鍵並且輸入「-fno-objc-arc」關閉使用ARC




4. 加入CoreText.framework 到你的專案中。

#import <CoreText/CoreText.h>





Have you included CoreText.framework in your project?


5. 在你需要兩種以上顏色的UILabel換成TTTAttributedLabel


 
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    
     cell = [self getCellContentView: CellIdentifier];
    
    
    TTTAttributedLabel *masterLabel = ((TTTAttributedLabel *)[cell viewWithTag: 2]);
    
    NSString* text = @"這是我的名片,請多指教。";
    NSMutableAttributedString * tttstring = [[NSMutableAttributedString alloc] initWithString:text];
    
    
    //字型與字體大小
    NSRange textRange = [text rangeOfString:text];
    UIFont *textSystemFont = [UIFont fontWithName: @"HeiTi TC" size: 18] ;
    CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)textSystemFont.fontName, textSystemFont.pointSize, NULL);
    if(italicFont){
        [tttstring addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:textRange];
    }
    CFRelease(italicFont);
    
    //紅字
    NSRange redRange = [text rangeOfString:@"名片"];
    [tttstring addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:redRange];
    
    //底線
    NSRange underlinRange = [text rangeOfString:@"請多指教"];
    [tttstring addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithBool:YES] range:underlinRange];
    
    //組體
    UIFont *boldSystemFont = [UIFont boldSystemFontOfSize: 18] ;
    CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    if(boldFont){
        [tttstring addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:underlinRange];
    }
    CFRelease(boldFont);
    
    
    masterLabel.frame = MASTER_RECT;
    masterLabel.numberOfLines = 1;
    [masterLabel setBackgroundColor:[UIColor clearColor]];//clearTextBackgroundColor!
    [masterLabel setText:tttstring];

    
    return cell;
}
 
#pragma mark -
#pragma mark custom cell view

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier 
{
    CGRect cellFrame = (self.mainTableView.style == UITableViewStylePlain)?  CGRectMake(0, 0, 320, 58) : CGRectMake(0, 0, 300, 58);
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
    cell.frame = cellFrame;
      
    
    TTTAttributedLabel *master = [[TTTAttributedLabel alloc] initWithFrame: MASTER_RECT];
 master.tag = 2;
 master.numberOfLines = 1;
 master.lineBreakMode = UILineBreakModeWordWrap;
    
    [master setFont: [UIFont fontWithName: @"HeiTi TC" size: 18]];
    

 [cell.contentView addSubview: master];

 return cell;
}

結果顯示


2012年6月5日 星期二

iOS5 NavigationBar Custom

ios 5 UINavigationBar bar,UITool bar 等添加背景图

iOS开发 XCode4 iOS5 改变UINavigationController的UINavigationBar的高度和背景图片 V1

自定义UINavigationBar的新类别:
  

@interface ViewController : UIViewController

@end

@interface UINavigationBar (BackgoundImage)  

@end  
在新类别的实现中,覆盖原有类的方法 - (void)drawRect:(CGRect)rect :
  

@interface ViewController ()

@end

@implementation UINavigationBar (BackgoundImage)  

- (void)layoutSubviews {  
    CGRect barFrame = self.frame;  
    barFrame.size.height = 100.0; //新的高度  
    self.frame = barFrame;  
}  

//其实只需要覆盖该方法就行,把self.frame.size.height改成100.0就OK  
- (void)drawRect:(CGRect)rect {  
    UIImage *image = [UIImage imageNamed:@"unit_nb.png"];  
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
}  

@end  
storyboard調整要放在viewDidAppear上 放在viewWillDidAppear上 TabBar切換會有問題

2012年5月22日 星期二

iOS5 push selectedIndex

//for prepareForSegue 
#import "NextPage.h" 


#pragma mark - Segues
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    // Make sure we're referring to the correct segue
    if ([[segue identifier] isEqualToString:@"PushNextPage"]) {
        
        // Get reference to the destination view controller
        WordBankVocabulary *vc = [segue destinationViewController];
        
        // get the selected index
        NSLog(@"get the selected index");
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        
        
        // Pass the name and index of our film
        [vc setSelectedIndex:selectedIndex];
        
        NSLog(@"prepareForSegue: %d",selectedIndex+1);
        
    }
    NSLog(@"prepareForSegue: run");
    
}
NextPage
@interface WordBankVocabulary : UITableViewController
{
      NSInteger selectedIndex;
}

@property (nonatomic) NSInteger selectedIndex;

@synthesize selectedIndex;

#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    NSString *textSelectedIndex =   [[NSString alloc] initWithFormat: @"%d",selectedIndex];

    cell.textLabel.text = textSelectedIndex;
    
    return cell;
}