Skip to content

Commit

Permalink
处理定位时 兼容手机语言 让获得的定位城市名字永远是中文名
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunyang committed Jul 28, 2016
1 parent 2d06b7d commit b3d290e
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 23 deletions.
8 changes: 8 additions & 0 deletions MobileProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,8 @@
34F49B2F1D261AE600FA003C /* MPBaseNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F49B2D1D261AE600FA003C /* MPBaseNavigationController.m */; settings = {ASSET_TAGS = (); }; };
34F49B321D262AA400FA003C /* MPHomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F49B311D262AA400FA003C /* MPHomeViewController.m */; settings = {ASSET_TAGS = (); }; };
34F49B331D262AA400FA003C /* MPHomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F49B311D262AA400FA003C /* MPHomeViewController.m */; settings = {ASSET_TAGS = (); }; };
34FFA1F21D49128100A7C342 /* NSString+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = 34FFA1F11D49128100A7C342 /* NSString+Additions.m */; settings = {ASSET_TAGS = (); }; };
34FFA1F31D49128100A7C342 /* NSString+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = 34FFA1F11D49128100A7C342 /* NSString+Additions.m */; settings = {ASSET_TAGS = (); }; };
9014C40DD45909D3FC1F2EBC /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 533C667C4AE7D52B4A1E63C2 /* libPods.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -1542,6 +1544,8 @@
34F49B2D1D261AE600FA003C /* MPBaseNavigationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPBaseNavigationController.m; sourceTree = "<group>"; };
34F49B301D262AA400FA003C /* MPHomeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPHomeViewController.h; sourceTree = "<group>"; };
34F49B311D262AA400FA003C /* MPHomeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPHomeViewController.m; sourceTree = "<group>"; };
34FFA1F01D49128100A7C342 /* NSString+Additions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+Additions.h"; sourceTree = "<group>"; };
34FFA1F11D49128100A7C342 /* NSString+Additions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+Additions.m"; sourceTree = "<group>"; };
533C667C4AE7D52B4A1E63C2 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
A8A48D3EEAEFFB901D36D8E8 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -1846,6 +1850,8 @@
341CA90E1D3097AF008B85D3 /* MBProgressHUD+MP.m */,
3403B0BC1D367954007CD7EF /* UITextField+Extension.h */,
3403B0BD1D367954007CD7EF /* UITextField+Extension.m */,
34FFA1F01D49128100A7C342 /* NSString+Additions.h */,
34FFA1F11D49128100A7C342 /* NSString+Additions.m */,
);
path = Other;
sourceTree = "<group>";
Expand Down Expand Up @@ -4215,6 +4221,7 @@
340D879A1D3F6310003D9C88 /* dateTimeHelper.m in Sources */,
34E08B2A1C3BFB90005C6CA3 /* NSString+RegexCategory.m in Sources */,
341C07D61D41A27E00FF905C /* BaseRequestService.m in Sources */,
34FFA1F31D49128100A7C342 /* NSString+Additions.m in Sources */,
34E08B2B1C3BFB90005C6CA3 /* UIWebView+Canvas.m in Sources */,
34E08B2C1C3BFB90005C6CA3 /* LogInViewController.m in Sources */,
34E08B2D1C3BFB90005C6CA3 /* QBImagePickerController.m in Sources */,
Expand Down Expand Up @@ -4625,6 +4632,7 @@
34E893851C3B9A03001327C8 /* NSException+Trace.m in Sources */,
3403B0BE1D367954007CD7EF /* UITextField+Extension.m in Sources */,
34E893FB1C3B9A03001327C8 /* UILabel+SuggestSize.m in Sources */,
34FFA1F21D49128100A7C342 /* NSString+Additions.m in Sources */,
34E8937F1C3B9A03001327C8 /* NSDictionary+Block.m in Sources */,
34E8937A1C3B9A03001327C8 /* NSDate+Reporting.m in Sources */,
342582641D3736DA003906BB /* UIView+EaseBlankPage.m in Sources */,
Expand Down
23 changes: 23 additions & 0 deletions MobileProject/Expand/Category/Other/NSString+Additions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NSString+Additions.h
// MobileProject 用法可参照https://github.com/johnil/VVeboTableViewDemo
//
// Created by wujunyang on 16/7/28.
// Copyright © 2016年 wujunyang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>

@interface NSString (Additions)

//计算大小带间距
- (CGSize)sizeWithConstrainedToWidth:(float)width fromFont:(UIFont *)font1 lineSpace:(float)lineSpace;
- (CGSize)sizeWithConstrainedToSize:(CGSize)size fromFont:(UIFont *)font1 lineSpace:(float)lineSpace;

//绘画文字
- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height andWidth:(float)width;

- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height;

@end
100 changes: 100 additions & 0 deletions MobileProject/Expand/Category/Other/NSString+Additions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// NSString+Additions.m
// MobileProject
//
// Created by wujunyang on 16/7/28.
// Copyright © 2016年 wujunyang. All rights reserved.
//

#import "NSString+Additions.h"

@implementation NSString (Additions)

- (CGSize)sizeWithConstrainedToWidth:(float)width fromFont:(UIFont *)font1 lineSpace:(float)lineSpace{
return [self sizeWithConstrainedToSize:CGSizeMake(width, CGFLOAT_MAX) fromFont:font1 lineSpace:lineSpace];
}

- (CGSize)sizeWithConstrainedToSize:(CGSize)size fromFont:(UIFont *)font1 lineSpace:(float)lineSpace{
CGFloat minimumLineHeight = font1.pointSize,maximumLineHeight = minimumLineHeight, linespace = lineSpace;
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)font1.fontName,font1.pointSize,NULL);
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;
//Apply paragraph settings
CTTextAlignment alignment = kCTLeftTextAlignment;
CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[6]){
{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
{kCTParagraphStyleSpecifierMinimumLineHeight,sizeof(minimumLineHeight),&minimumLineHeight},
{kCTParagraphStyleSpecifierMaximumLineHeight,sizeof(maximumLineHeight),&maximumLineHeight},
{kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(linespace), &linespace},
{kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(linespace), &linespace},
{kCTParagraphStyleSpecifierLineBreakMode,sizeof(CTLineBreakMode),&lineBreakMode}
},6);
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)font,(NSString*)kCTFontAttributeName,(__bridge id)style,(NSString*)kCTParagraphStyleAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes];
// [self clearEmoji:string start:0 font:font1];
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)string;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
CGSize result = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [string length]), NULL, size, NULL);
CFRelease(framesetter);
CFRelease(font);
CFRelease(style);
string = nil;
attributes = nil;
return result;
}

- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height andWidth:(float)width{
CGSize size = CGSizeMake(width, font.pointSize+10);
CGContextSetTextMatrix(context,CGAffineTransformIdentity);
CGContextTranslateCTM(context,0,height);
CGContextScaleCTM(context,1.0,-1.0);

//Determine default text color
UIColor* textColor = color;
//Set line height, font, color and break mode
CTFontRef font1 = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize,NULL);
//Apply paragraph settings
CGFloat minimumLineHeight = font.pointSize,maximumLineHeight = minimumLineHeight+10, linespace = 5;
CTLineBreakMode lineBreakMode = kCTLineBreakByTruncatingTail;
CTTextAlignment alignment = kCTLeftTextAlignment;
//Apply paragraph settings
CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[6]){
{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
{kCTParagraphStyleSpecifierMinimumLineHeight,sizeof(minimumLineHeight),&minimumLineHeight},
{kCTParagraphStyleSpecifierMaximumLineHeight,sizeof(maximumLineHeight),&maximumLineHeight},
{kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(linespace), &linespace},
{kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(linespace), &linespace},
{kCTParagraphStyleSpecifierLineBreakMode,sizeof(CTLineBreakMode),&lineBreakMode}
},6);

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)font1,(NSString*)kCTFontAttributeName,
textColor.CGColor,kCTForegroundColorAttributeName,
style,kCTParagraphStyleAttributeName,
nil];
//Create path to work with a frame with applied margins
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path,NULL,CGRectMake(p.x, height-p.y-size.height,(size.width),(size.height)));

//Create attributed string, with applied syntax highlighting
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes];
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)attributedStr;

//Draw the frame
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,CFAttributedStringGetLength(attributedString)),path,NULL);
CTFrameDraw(ctframe,context);
CGPathRelease(path);
CFRelease(font1);
CFRelease(framesetter);
CFRelease(ctframe);
[[attributedStr mutableString] setString:@""];
CGContextSetTextMatrix(context,CGAffineTransformIdentity);
CGContextTranslateCTM(context,0, height);
CGContextScaleCTM(context,1.0,-1.0);
}

- (void)drawInContext:(CGContextRef)context withPosition:(CGPoint)p andFont:(UIFont *)font andTextColor:(UIColor *)color andHeight:(float)height{
[self drawInContext:context withPosition:p andFont:font andTextColor:color andHeight:height andWidth:CGFLOAT_MAX];
}


@end
2 changes: 2 additions & 0 deletions MobileProject/Expand/Macros/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
#define isIOS6 ([[[UIDevice currentDevice]systemVersion]floatValue] < 7.0)
// 是否大于等于IOS8
#define isIOS8 ([[[UIDevice currentDevice]systemVersion]floatValue] >=8.0)
// 是否大于IOS9
#define isIOS9 ([[[UIDevice currentDevice]systemVersion]floatValue] >=9.0)
// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

Expand Down
61 changes: 38 additions & 23 deletions MobileProject/Resource/AppDelegate/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,48 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
else
{
DDLogError(@"定位信息:%f,%f",loction.location.coordinate.latitude,loction.location.coordinate.longitude);

CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:loction.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

//处理手机语言 获得城市的名称(中文)
NSMutableArray *userDefaultLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [userDefaultLanguages objectAtIndex:0];
//如果不是中文 则强制先转成中文 获得后再转成默认语言
if (![currentLanguage isEqualToString:@"zh-Hans"]&&![currentLanguage isEqualToString:@"zh-Hans-CN"]) {
//IOS9前后区分
if (isIOS9) {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans-CN", nil] forKey:@"AppleLanguages"];
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", nil] forKey:@"AppleLanguages"];
}
}

//转换地理信息
if (placemarks.count>0) {
CLPlacemark *placemark=[placemarks objectAtIndex:0];
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}

NSLog(@"百度当前城市:[%@]",city);

// 城市名传出去后,立即 Device 语言 还原为默认的语言
[[NSUserDefaults standardUserDefaults] setObject:userDefaultLanguages forKey:@"AppleLanguages"];
}
}];
}
}];

//百度定位并获取相关城市信息
// [[MPLocationManager shareInstance] startBMKLocationWithReg:^(BMKUserLocation *loction, NSError *error) {
// if (!error) {
// CLGeocoder *geocoder=[[CLGeocoder alloc]init];
// [geocoder reverseGeocodeLocation:loction.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// if (placemarks.count>0) {
// CLPlacemark *placemark=[placemarks objectAtIndex:0];
// //获取城市
// NSString *city = placemark.locality;
// if (!city) {
// //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
// city = placemark.administrativeArea;
// }
//
// NSLog(@"百度当前城市:[%@]",city);
// }
// }];
// }
// }];

//系统自带定位
//[[MPLocationManager shareInstance] startSystemLocationWithRes:^(CLLocation *loction, NSError *error) {
// DDLogError(@"系统自带定位信息:%f,%f",loction.coordinate.latitude,loction.coordinate.longitude);
//}];
// [[MPLocationManager shareInstance] startSystemLocationWithRes:^(CLLocation *loction, NSError *error) {
// DDLogError(@"系统自带定位信息:%f,%f",loction.coordinate.latitude,loction.coordinate.longitude);
// }];



Expand Down

0 comments on commit b3d290e

Please sign in to comment.