蓝懿ios技术交流和心得分享16,1.12

在iOS中,经常遇到需要根据字符串的内容动态指定UILabel,UITextView,UITableViewCell等的高度的情况,这个时候就需要动态的计算字符串内容的高度,下面是计算的方法:

[cpp] view plain

copy

  1. /**  
  •  @method 获取指定宽度情况ixa,字符串value的高度

 

  •  @param value 待计算的字符串

 

  •  @param fontSize 字体的大小

 

  •  @param andWidth 限制字符串显示区域的宽度

 

  •  @result float 返回的高度

 

  •  */

  

  • - (float

) heightForString:(NSString *)value fontSize:(

float

)fontSize andWidth:(

float

)width  

  • {  
  •     CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];//此处的换行类型(lineBreakMode)可根据自己的实际情况进行设置

  

  •     return

 sizeToFit.height;  

 

前不久QA报了个文字显示不全的bug,我看了下代码,发现是计算高度出了问题。之前的同事在UITableViewCell中使用了UITextView,但是计算高度时使用了和UILabel相同的的方法。

 

其实UITextView在上下左右分别有一个8px的padding,当使用[NSString sizeWithFont:constrainedToSize:lineBreakMode:]时,需要将UITextView.contentSize.width减去16像素(左右的padding 2 x 8px)。同时返回的高度中再加上16像素(上下的padding),这样得到的才是UITextView真正适应内容的高度。

 

示例代码如下:

[html]

 view plaincopy

  1. + (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{  
  •     float fPadding

 = 

16

.0; // 8.0px x 2  

  •     CGSize constraint

 = 

CGSizeMake

(textView.contentSize.width - fPadding, CGFLOAT_MAX);  

  •    
  •     CGSize size

 = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  

  •    
  •     float fHeight

 = 

size

.height + 16.0;  

  •    
  •     return fHeight;  
  • }  

1.创建一个UITableController

2.为每个Tab创建一个视图控制器

3.创建一个array  把视图控制器都添加到array中 在把array放到UITabBarCtrl的Controllers中

4.设置根视图为tabBar

*只有在支持旋转的设置  才会旋转  发生旋转时只有当前的ViewController才会接受到旋转的消息

*对于UITabBar自带的tabBar  不能直接去修改

//修改背景图片

UIImage *tabBackground = [UIImage imageNamed:@"main_title2.png"];

   if ([tabBarController.tabBar respondsToSelector:@selector(setBackgroundImage:)]){

        [tabBarController.tabBar setBackgroundImage:tabBackground];

    }

常用委托

//当点击tabBarItem时触发该操作  

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController {

    NSLog(@"%d", viewController.tabBarItem.tag);

}

通过代理我们可以监测UITabBarController的当前选中viewController的变化,以及moreViewController中对编辑所有viewController的编辑。通过实现下面方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

  该方法用于控制TabBarItem能不能选中,返回NO,将禁止用户点击某一个TabBarItem被选中。但是程序内部还是可以通过直接setSelectedIndex选中该TabBarItem。

  下面这三个方法主要用于监测对moreViewController中对view controller的edit操作

 

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;

 

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

 

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

 

改变UITabBarController中当前显示的viewController

 1、selectedIndex属性

  通过该属性可以获得当前选中的viewController,设置该属性,可以显示viewControllers中对应的index的viewController。如果当前选中的是MoreViewController的话,该属性获取出来的值是NSNotFound,而且通过该属性也不能设置选中MoreViewController。设置index超出viewControllers的范围,将会被忽略。

  2、selectedViewController属性

  通过该属性可以获取到当前显示的viewController,通过设置该属性可以设置当前选中的viewController,同时更新selectedIndex。可以通过给该属性赋值

tabBarController.moreNavigationController可以选中moreViewController。

  3、viewControllers属性

  设置viewControllers属性也会影响当前选中的viewController,设置该属性时UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相同的viewController,如果该index无效的话,则默认选中第一个viewController。

 

 

UITabBarItem

//小红圈提示

barItem1.badgeValue = @"1";

//给选中的状态和未选中的状态指定不同的图片

[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] 

   withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];

 

//每个UIController中都有一个navigationItem和一个tabBarItem 一般创建一个相应类型的对象  对其赋值

//系统样式

    UITabBarItem * barItem1 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];

    barItem1.badgeValue = @"1";

    yvCtrl.tabBarItem = barItem1;

//自定义样式

firstController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"img.png"] tag:101]

 

//重写ViewController里的init   在初始化里添加Item

(id)init {  

    if ([super init] != nil) {  

        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1];  

        self.tabBarItem = item;  

        [item release];  

    }  

    return self;  

创建一个UITabConroller:

.h文件中:

[cpp] view plain

copy

  1. #import<UIKit/UIKit.h>  
  •   
  • @interface AppDelegate :UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>  
  • {  
  •     UITabBarController*     tabBarController;  
  • }  
  •   
  • @property (strong,nonatomic)UIWindow *window;  
  • @property (nonatomic,retain) UITabBarController*     tabBarController;  
  • -(void

) initTabBarController;  

  •   
  • @end  

.m文件中:

[cpp]

 view plaincopy

  1. -(

void

) initTabBarController  

  • {  
  •    // create a tab bar controller

  

  •    self.tabBarController = [[UITabBarControlleralloc]init];  
  •     self.tabBarController.delegate =self;  
  •    //1====>navhomeController

  

  •     HomeController* homeView = [[HomeControlleralloc]initWithNibName :@"HomeController"

bundle : nil];  

  •     UINavigationController* navhomeController = [[UINavigationControlleralloc]initWithRootViewController : homeView];  
  •     [homeView release];  
  •     [navhomeController.navigationBarcustomed];  
  •     [navhomeController.navigationBarcustomedBarBackground];  
  •     navhomeController.navigationBar.topItem.title =@"文件查看"

;  

  •     navhomeController.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"文件查看"

 image:[UIImageimageNamed:@

"home.png"

] tag :0];  

  •       
  •    TempViewController* temp1 = [[TempViewControlleralloc]initWithNibName:@"TempViewController"

bundle:nil];  

  •    UINavigationController* navTempController = [[UINavigationControlleralloc]initWithRootViewController : temp1];  
  •     [temp1 release];  
  •     navTempController.navigationBar.topItem.title =@"最近"

;  

  •     navTempController.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"最近"

image:[UIImageimageNamed:@

"icon_bookmark.png"

] tag :1];  

  •       
  •    DesktopViewController* desktopView = [[DesktopViewControlleralloc]initWithNibName:@"DesktopViewController"

bundle:nil];  

  •     UINavigationController* navDesktopView = [[UINavigationControlleralloc]initWithRootViewController : desktopView];  
  •     [desktopView release];  
  •     [navDesktopView.navigationBarcustomed];  
  •     navDesktopView.navigationBar.topItem.title =@"桌面"

;  

  •     navDesktopView.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"桌面"

image:[UIImageimageNamed:@

"icon_attention.png"

] tag :2];  

  •       
  •    TempViewController* temp3 = [[TempViewControlleralloc]initWithNibName:@"TempViewController"

bundle:nil];  

  •    UINavigationController* navTempController3 = [[UINavigationControlleralloc]initWithRootViewController : temp3];  
  •     [temp3 release];  
  •     navTempController3.navigationBar.topItem.title =@"搜索"

;  

  •     navTempController3.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"搜索"

image:[UIImageimageNamed:@

"icon_search.png"

] tag :3];  

  •       
  •       
  •    //5====>SettingController

  

  •     SettingController* settingView = [[SettingControlleralloc]initWithNibName :@"SettingController"

bundle : nil];  

  •     UINavigationController* navSettingView = [[UINavigationControlleralloc]initWithRootViewController : settingView];  
  •     [settingView release];  
  •     navSettingView.navigationBar.topItem.title =@"设置"

;  

  •     navSettingView.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"设置"

image:[UIImageimageNamed:@

"icon_setting.png"

] tag :4];  

  •       
  •     tabBarController.viewControllers = [NSArrayarrayWithObjects : navhomeController,navTempController,navDesktopView,navTempController3,navSettingView,nil];  
  •       
  •     OCRELEASE(navhomeController);  
  •     OCRELEASE(navTempController);  
  •     OCRELEASE(navDesktopView);  
  •     OCRELEASE(navTempController3);  
  •     OCRELEASE(navSettingView);  
  •       
  •    // add navigation controllers to the tab bar controller

  

  •    //    tabBarController.viewControllers = [NSArray arrayWithObjects : navhomeController,navPersonalController,navAttentionView,navSeekView,navSettingView,nil];

  

  •    //    OCRELEASE(navhomeController);

  

  •    //    OCRELEASE(navPersonalController);

  

  •    //    OCRELEASE(navAttentionView);

  

  •    //    OCRELEASE(navSeekView);

  

  •    //    OCRELEASE(navSettingView);

  

  • }  
  • @synthesize tabBarController;  
  •   
  • - (void

)dealloc  

  • {  
  •     [_windowrelease];  
  •     [tabBarControllerrelease];  
  •     [superdealloc];  
  • }  
  •   
  • - (BOOL

)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  

  • {  
  •    self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];  
  •    // Override point for customization after application launch.

  

  •     [selfinitTabBarController];  
  •       
  •    self.window.rootViewController =tabBarController;  
  •     [self.windowmakeKeyAndVisible];  
  •    returnYES;  
  • }  
  • 学习ios  重要还是要理清楚思路  在做或者看老师代码的时候 自己多想想为什么  不要自己看着就抄       另外还是要推荐一下 蓝懿IOS这个培训机构  和刘国斌老师刘国斌老师还是很有名气的,听朋友说刘老师成立了蓝懿iOS,,老师讲课方式很独特,能够尽量让每个人都能弄明白,有的比较难懂的地方,如果有的地方还是不懂得话,老师会换个其它方法再讲解,这对于我们这些学习iOS的同学是非常好的,多种方式的讲解会理解得更全面,这个必须得给个赞,嘻嘻,还有就是这里的学习环境很好,很安静,可以很安心的学习,安静的环境是学习的基础,小班讲课,每个班20几个学生,学习氛围非常好,每天都学到9点多才离开教室,练习的时间很充裕,而且如果在练习的过程中有什么困难,随时可以向老师求助,不像其它机构,通过视频教学,有的甚至学完之后都看不到讲师本人,问点问题都不方便,这就是蓝懿与其它机构的区别,相信在刘国斌老师的细心指导下,每个蓝懿学员都能找到满意的工作,加油!

                                                                  写博客第九十四天;

                                                                              QQ:565803433

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值