有沒有一種方便的方法來製作帶有徽章的按鈕?不在圖標上...我想在應用程序內執行此操作。程序選擇了徽章的按鈕或圖像。或者我必須建立一個photoshop圖像庫?添加徽章圖標以圓形矩形按鈕?
3
A
回答
3
如果沒有photoshop圖像庫,您需要使用resizableImageWithCapInsets
來實現此目的。有一些很好的線索(here和here)解釋了它的用法。
這是我剛纔給你一個想法的例子:
//Create a label (width/height not important at this stage)
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
yourLabel.text = @"7+";
[yourLabel sizeToFit];
CGRect labelFrame = yourLabel.frame;
//Here we create a UIImage that is resizable, but will not resize the areas concerned with the cap insets you've defined
UIImage *badgeImage = [[UIImage imageNamed:@"badge.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImageView *badgeImageView = [[UIImageView alloc]initWithImage:badgeImage];
badgeImageView.contentMode = UIViewContentModeScaleToFill;
badgeImageView.backgroundColor = [UIColor clearColor];
labelFrame.size.width += 5; //This is the 'padding' on the right and left (added together)
//If your badge edges are completely circular then you don't want to change the height, but if they're not then go ahead in the same way with the width. If your badge has a static height, you'll need to make sure the font size doesn't exceed this height; better start off with a small font-size
badgeImageView.frame = labelFrame; //The badge is now the right width with padding taken into account
//Center the label on the badge image view
yourLabel.center = CGPointMake(badgeImageView.frame.size.width/2, badgeImageView.frame.size.height/2);
//Finally we add the label to the badge image view
[badgeImageView addSubview:yourLabel];
//Add your badge to the main view
[self.view addSubview:badgeImageView];
[badgeImageView release];
[yourLabel release];
+0
非常感謝你......我會嘗試一下。 – 2012-04-24 06:58:04
0
UILabel *lbl_notification_count = [[UILabel alloc]initWithFrame:CGRectMake(5,0, 18, 18)];
lbl_notification_count.textColor = [UIColor whiteColor];
lbl_notification_count.textAlignment = NSTextAlignmentCenter;
lbl_notification_count.text = [NSString stringWithFormat:@"%d",appDel.NotificationBadge];
lbl_notification_count.adjustsFontSizeToFitWidth=YES;
lbl_notification_count.layer.borderWidth = 1;
lbl_notification_count.layer.cornerRadius = lbl_notification_count.layer.frame.size.height/2;
lbl_notification_count.layer.masksToBounds = YES;
lbl_notification_count.layer.borderColor =[[UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0] CGColor];
lbl_notification_count.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0];
lbl_notification_count.font = CustomFontMediumWithSize(12);
if (appDel.NotificationBadge >= 1) {
[btnNotification addSubview:lbl_notification_count];
[lbl_notification_count setHidden:NO];
}else{
[lbl_notification_count setHidden:YES];
}
相關問題
- 1. 如何在圓形矩形按鈕中添加箭頭圖標
- 2. 使用圓形矩形按鈕作爲帶有計數器的徽章
- 3. 如何將4個按鈕添加到矩形/圓形
- 4. Xcode 5圓形矩形按鈕
- 5. 圓形矩形按鈕連接到ViewController.h
- 6. NSToolbar中的圓形矩形按鈕
- 7. 按鈕圖標:圓形關閉圖標
- 8. 帶有右下圓形彩色徽章的圓形ImageView
- 9. 如何添加流血像信封或圓形徽章自定義形狀?
- 10. 圓形按鈕
- 11. 圓形按鈕
- 12. 圓形按鈕
- 13. 添加圖標,形成按鈕
- 14. 帶圖標的Android圓形按鈕
- 15. 圓形按鈕組視圖
- 16. 圓形按鈕與圖像
- 17. Android:添加按鈕和矩形佈局
- 18. 圓形按鈕形成
- 19. 如何使用Google課堂分享按鈕的圓形徽標?
- 20. ImageMagick:創建透明的矩形圖像並在中心添加方形徽標
- 21. 矩形以外形狀的按鈕
- 22. UIImage按鈕(圓形按鈕)
- 23. 長方形按鈕,頂部的圓形圖標中的Android
- 24. 閱讀多個圓形矩形按鈕的文本
- 25. 如何更改別針按鈕(Pinterest)背景矩形爲圓形?
- 26. 在iPhone中使用圓形矩形按鈕用於TableView頁腳?
- 27. iOS PopOver - 僅限酒吧按鈕項目或圓形矩形?
- 28. XCode圓形矩形按鈕的作用不可靠
- 29. StateHighlighted上更改圓形矩形按鈕背景色
- 30. Qt添加一個圓形矩形到一個圖形項目組
你說的是徽章會自動調節到文本的寬度是多少? – sooper 2012-04-23 23:56:55