這是一個很直覺跟貼心的功能 至於要怎麼實作呢?
我直接去google一下了 發現有一個很好用的TTTAttributedLabel
所以就不打算硬刻一個uilabel出來 而且這個超方便跟好用
還有如果從自己的app去開啟到其他app的方法是透過url scheme
這邊也剛好用到~不過map在ios6上的用法就不一樣了
以下範例是打電話跟開地圖(support ios6)
//一些初始化的設定
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
label.font = [UIFont systemFontOfSize:14.0f];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeCharacterWrap;
label.delegate = self;
//加上電話連結
NSRange range = [label.text rangeOfString:[data objectForKey:@"telephone"]];
[label addLinkToPhoneNumber:[data objectForKey:@"telephone"] withRange:range];
//加地址連結
range = [label.text rangeOfString:[data objectForKey:@"address"]];
NSDictionary *addressBook = @{ @"name": [data objectForKey:@"name"],
@"lat": [data objectForKey:@"lat"],
@"long": [data objectForKey:@"long"]
};
[label addLinkToAddress:addressBook withRange:range];
/** 這邊就要實作點擊連結後的動作 **/
//電話連結
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{
NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNumber]]; [[UIApplication sharedApplication] openURL:phoneURL];}
//地址連結
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithAddress:(NSDictionary *)addressComponents
{
NSString *title = [addressComponents objectForKey:@"name"];
float latitude = [[addressComponents objectForKey:@"lat"] floatValue];
float longitude = [[addressComponents objectForKey:@"long"] floatValue];
//for iOS6 later
if( [MKMapItem class]) { MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil]; MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark]; mapItem.name = title;
[mapItem openInMapsWithLaunchOptions:nil];
} else {
int zoom = 13;
NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@@%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
}
最後效果如下:
參考的連結:
http://stackoverflow.com/questions/8839464/uilabel-string-as-text-and-links
http://www.techotopia.com/index.php/Integrating_Maps_into_iPhone_iOS_6_Applications_using_MKMapItem#MKMapItem_and_MKPlacemark_Classes
https://github.com/mattt/TTTAttributedLabel
http://mattt.github.com/TTTAttributedLabel/Classes/TTTAttributedLabel.html
0 意見:
張貼留言