以下哪项将设置UISegmentedControl的字体?

时间:2016-07-28 08:08:29

标签: ios uisegmentedcontrol

  • UIFont font = [UIFont boldSystemFontOfSize: 12.0f]; NSDictionary attributes = [NSDictionary dictionaryWithObject: font forKey: UITextAttributeFont]; [segmentedControl setTitleTextAttributes: attributes forState: UIControlStateNormal];

  • [[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

  • segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);

  • 这些都没有。

3 个答案:

答案 0 :(得分:2)

private MediaRecorder mMediaRecorder;
private static final long UPDATE_INTERVAL = 100;
private Sync mSync;
private boolean mIsRecording = false;
private long mStartTime = 0;
private long mStopTime = 0;
private String mFileName;

private void startRecord() {
    startRecording();
    mSync = new Sync(new Runnable() {

        @Override
        public void run() {
            if (!mIsRecording||!isAdded())
                return;
            long currentTime = System.currentTimeMillis();
            if (currentTime-mStartTime>AppConstants.MAX_RECORD_DURATION_S*1000){
                stopRecord();
                updateLayoutVisibility();
                return;                                 
            }
            String str = getString(R.string.recording_template,(currentTime-mStartTime)/1000,AppConstants.MAX_RECORD_DURATION_S);
            mTextSeconds.setText(str);
            mSync.next();
        }
    },UPDATE_INTERVAL);
    mSync.now();
    mLayoutButtonsStartRecord.setVisibility(View.GONE);
    mLayoutButtonsRecord.setVisibility(View.VISIBLE);
}


private void stopRecord() {
    mSync.stop();
    mIsRecording = false;
    mStopTime = System.currentTimeMillis();
    mMediaRecorder.stop();
    mMediaRecorder.release();
    mMediaRecorder = null;      
}

private void startRecording() {
    if (mMediaRecorder != null) {
        mMediaRecorder.release();
    }
    mFileName = getTempRecordFileName();
    if (mFileName==null){
        dismiss();
        return;
    }
    mIsRecording = true;
    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setAudioSource(AudioSource.MIC);
    mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(AudioEncoder.AAC);
    mMediaRecorder.setAudioChannels(1);

    mMediaRecorder.setOutputFile(mFileName);
    try {
        mMediaRecorder.prepare();
        mMediaRecorder.start();
        mStartTime = System.currentTimeMillis();
    } catch (IOException e) {
        Toast.makeText(getActivity(),
                R.string.error_failed_start_recording, Toast.LENGTH_LONG)
                .show();
        Log.d(TAG, "Failed start recorder", e);
        mMediaRecorder.release();
        mMediaRecorder = null;
        dismiss();
        return;
    }
}

private class Sync{
    private Handler handler = new Handler();
    private Runnable task;
    private long period;

    public Sync(Runnable task,long period){
        this.task = task;
        this.period = period;
        handler.removeCallbacks(task);
    }
    public void now(){
        task.run();
    }

    public void next(){
        handler.postDelayed(task, period);
    }
    public void stop(){
        handler.removeCallbacks(task);
    }
}

@Override
public void onPause() {
    super.onPause(); 
    if (mMediaRecorder != null) {
        mMediaRecorder.release();
        mMediaRecorder = null;
    }
    if (mSync!=null){
       mSync.stop();
    }

}

上面的代码将更改一个特定segmentcontrol的字体,而下面的代码将更改应用中所有segmentcontrol的字体。

UIFont font = [UIFont boldSystemFontOfSize: 12.0f];
NSDictionary attributes = [NSDictionary dictionaryWithObject: font forKey: UITextAttributeFont];
[segmentedControl setTitleTextAttributes: attributes forState: UIControlStateNormal];

此代码[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; 只会缩小您的segmentcontrol

注意:

只需将已弃用的segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);替换为UITextAttributeFont即可正常使用。

答案 1 :(得分:0)

你可以做点什么,

 [segment setTitleTextAttributes:@{NSFontAttributeName :[UIFont fontWithName:@"HelveticaNeue" size:17.0], NSForegroundColorAttributeName : [UIColor darkGrayColor] } forState:UIControlStateNormal];

此处segmentUISegmentedControl对象。

答案 2 :(得分:0)

在上班测试中,这两种变体是正确的:

  • UIFont font = [UIFont boldSystemFontOfSize:12.0f]; NSDictionary attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
  • [[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@“STHeitiSC-Medium”size:13.0],UITextAttributeFont,nil] forState:UIControlStateNormal];